<?php

namespace app\api\controller\v2;

use app\admin\model\cms\AlbumModel;
use app\admin\model\cms\Channels;
use app\admin\model\cms\Chart;
use app\admin\model\cms\ChartTableModel;
use app\admin\model\cms\Diyform;
use app\admin\model\cms\Live;
use app\admin\model\cms\LiveCategoryModel;
use app\admin\model\cms\MembershipExpiryRecordModel;
use app\admin\model\cms\News;
use app\admin\model\cms\NewsCategoryModel;
use app\admin\model\cms\OrderModel;
use app\admin\model\cms\Plate;
use app\admin\model\cms\PlazaCategorymodel;
use app\admin\model\cms\PlazaPostModel;
use app\admin\model\cms\RatingModel;
use app\admin\model\cms\Video;
use app\admin\model\cms\VideoCategoryModel;
use app\admin\model\CutWordIndex;
use app\admin\model\Investment;
use app\admin\model\InvestmentCategoryModel;
use app\admin\model\User;
use app\api\service\cms\AttachmentService;
use app\api\service\cms\CacheService;
use app\api\service\CommonService;
use app\api\service\FetchArticleService;
use app\api\service\HuaWeiLive;
use app\api\service\HuaWeiSMS;
use app\api\service\TengxunSignatureUtil;
use app\common\controller\Api;
use app\common\controller\Backend;
use app\common\model\Attachment;
use app\common\model\Channel;
use app\common\model\UserLike;
use app\common\model\UserFavorite;
use app\admin\model\cms\Comment;
use app\admin\model\cms\Platepost;
use think\Config;
use think\Controller;
use think\Db;
use think\db\Query;
use think\Model;
use think\Validate;
use think\View;
use function EasyWeChat\Kernel\Support\current_url;

/**
 * 广场
 */
class PlazaPost extends Api
{
    //无需登录的方法,同时也就不需要鉴权了
    protected $noNeedLogin = ['login', 'mobilelogin', 'getQrCodeLiveShow', 'getLiveForeshow', 'test'];
    //无需鉴权的方法,但需要登录
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    //首页广场
    public function homePlaza()
    {
        $page = $this->request->param('page', 1);//页码
        $limit = 10;//每页数量
        $type = $this->request->param('type', 'all');//类型all:全部,1:今日,2:七日内发布的内容,3:本月发布的,4:今年发布的
        $time = $this->request->param('time', null);//时间筛选(格式:2023-09-13)
        $orderway = $this->request->param('orderway', 'desc');//排序

        $start_time = $this->request->param('start_time', '');//开始时间
        $end_time = $this->request->param('end_time', '');//结束时间
        if ($start_time && $end_time) {
            $start_time = $start_time . ' 00:00:00';
            $end_time = $end_time . ' 23:59:59';
        } else {
            $start_time = '';
            $end_time = '';
        }

        $time_arr = [
            'time' => $time,
            'start_time' => $start_time,
            'end_time' => $end_time,
        ];

        $user = $this->auth->getUser();//用户信息
        $new_user = new User();
        $rule_column_id = $new_user->isChannelsPermission($user)['rule_column_id'];//当前用户拥有权限的的全部栏目id
        //检测自有分类id是否有查看权限
        $membershipExpiryRecord = new MembershipExpiryRecord();

        //获取债市论坛2级栏目
        $plate_channels_id = 3;
        $plate_category_table_name = 'plate';
        $plate_category_model = new Plate();

        //获取首页2级栏目
        $plate_channels = Channels::where('parent_id', $plate_channels_id)
            ->where('status', '>', 0)
            ->field('id,correlation_id')
            ->select();
        //当前全部自有分类的id(就是2级分类id)
        $plate_category_id = [];
        foreach ($plate_channels as $v) {
            $plate_category_id[] = $v['correlation_id'];
        }
        $plate_free_catedory_id_arr = [];//需要判断内容是否收费的自有分类id|默认空
        //判断顶级id不在在会员权限数组中
        if (!in_array($plate_channels_id, $rule_column_id)) {
            //不在会员权限数组中,说明用户组可能只有部分2级分类的权限
            //查对应的自有分类表需要付费的分类id
            $plate_pay_catedory_id = $plate_category_model->whereIn('id', $plate_category_id)->where('membership_price', '>', 0)->column('id');

            //查对应的自有分类表免费的分类id
            $plate_free_catedory_id_arr = $plate_category_model->whereIn('id', $plate_category_id)->where('membership_price', 0)->column('id');

            //判断用户是否购买了付费的分类,没买则把这个分类id从$category_id数组中删掉
            foreach ($plate_pay_catedory_id as $v) {
                //用户没买会员组也没买对应分类,经过判断得到用户可以看到的自有分类id
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'categories', $plate_category_table_name, $v);
                if ($isMemberExpire['code'] != 200) {
                    $plate_category_id = array_diff($plate_category_id, [$v]);//数组差集
                }
            }
        }

        $plate_data = $this->getPlateHome($user, $plate_category_id, $plate_free_catedory_id_arr, $page, $limit, '', $time_arr, $type, $orderway);


        //获取投研日志(数据库)2级栏目
        $investment_channels_id = 7;
        $investment_category_table_name = 'investment_category';
        $investment_category_model = new InvestmentCategoryModel();

        //获取首页2级栏目
        $investment_channels = Channels::where('parent_id', $investment_channels_id)
            ->where('status', '>', 0)
            ->field('id,correlation_id')
            ->select();
        //当前全部自有分类的id(就是2级分类id)
        $investment_category_id = [];
        foreach ($investment_channels as $v) {
            $investment_category_id[] = $v['correlation_id'];
        }
        $investment_free_catedory_id_arr = [];//需要判断内容是否收费的自有分类id|默认空


        //判断顶级id不在在会员权限数组中
        if (!in_array($investment_channels_id, $rule_column_id)) {
            //不在会员权限数组中,说明用户组可能只有部分2级分类的权限
            //查对应的自有分类表需要付费的分类id
            $investment_pay_catedory_id = $investment_category_model->whereIn('id', $investment_category_id)->where('membership_price', '>', 0)->column('id');

            //查对应的自有分类表免费的分类id
            $investment_free_catedory_id_arr = $investment_category_model->whereIn('id', $investment_category_id)->where('membership_price', 0)->column('id');

            //判断用户是否购买了付费的分类,没买则把这个分类id从$category_id数组中删掉
            foreach ($investment_pay_catedory_id as $v) {
                //用户没买会员组也没买对应分类,经过判断得到用户可以看到的自有分类id
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'categories', $investment_category_table_name, $v);
                if ($isMemberExpire['code'] != 200) {
                    $investment_category_id = array_diff($investment_category_id, [$v]);//数组差集
                } else {

                }
            }
        }

        $investment_data = $this->investmentHome($user, $investment_category_id, $investment_free_catedory_id_arr, $page, $limit, '', $time_arr, $type, $orderway);


//create_time create_time

        $res['plateList'] = array_merge($investment_data['investmentList'], $plate_data['plateList']);

        //时间倒序排列
        usort($res['plateList'], function ($a, $b) {
            return strtotime($b['create_time']) - strtotime($a['create_time']);
        });

        $res['total'] = $plate_data['total'] + $investment_data['total'];


        $this->success('', $res);
    }

    //获取投研日志-首页内容列表
    private function investmentHome($user, $category_id_arr, $free_catedory_id_arr, $page, $limit, $keyword, $time_arr, $type, $orderway, $membershipExpiryRecord = null)
    {

        $data = Investment::whereIn('investment_category_id', $category_id_arr);

        $is_show_file_category_id = InvestmentCategoryModel::whereIn('id', $category_id_arr)->where('type', 1)->column('id');//需要展示文件的分类id


        $start_time = $time_arr['start_time'];
        $end_time = $time_arr['end_time'];

        if ($start_time) {
            $data = $data->where('createtime', '>=', strtotime($start_time));
        }
        if ($end_time) {
            $data = $data->where('createtime', '<=', strtotime($end_time));
        }

        $getStartAndEndTime = $this->getStartAndEndTime($type);//根据类型获取日期时间
        $data->where('createtime', '>=', strtotime($getStartAndEndTime['start_time']));

        //$data = $data->order('createtime', 'desc')->paginate($limit, false, ['page' => $page])->toArray();
        $data = $data->order('createtime', 'desc')->paginate($limit, false, ['page' => $page])->toArray();


        $cache_service = new CacheService();

        $arList = [];
        foreach ($data['data'] as $v) {
            $v = $cache_service->showHashData('investment', $v['id']);
            $content_time = $v['content_time'] ?: $v['createtime'];
            $content_time = date('Y-m-d H:i', $content_time);//发布时间

            $is_show_file = 0;
            $file_list = [];
            if (in_array($v['investment_category_id'], $is_show_file_category_id)) {
                //判断是否分类是只显示文件的
                $is_show_file = 1;
                $file_list = json_decode($v['file_list'], true) ?? [];
                foreach ($file_list as &$fv) {
                    $fv['url'] = obsurl($fv['url']);
                }
            }

            $file_img = [];
            foreach (explode(',', $v['file_img'] ?? '') as $path) {
                $file_img[] = obsurl($path);
            }
            $file_video = [];
            foreach (explode(',', $v['file_video'] ?? '') as $path) {
                $file_video[] = obsurl($path);
            }
            $file_audio = [];
            foreach (explode(',', $v['file_audio'] ?? '') as $path) {
                $file_audio[] = obsurl($path);
            }
            $file_data = [];
            if ($v['file_data']) {
                $attachment = Attachment::whereIn('url', $v['file_data'])->select();
                foreach ($attachment as $av) {
                    $file_data[] = [
                        'filename' => $av['filename'],
                        'url' => obsurl($av['url'])
                    ];
                }
            }

            //内容数组
            $content_arr = [
                "table_name" => "investment",
                'id' => $v['id'],
                'macro_content' => $v['macro_content'],//宏观面
                'policy_content' => $v['policy_content'],//政策面
                'fund_content' => $v['fund_content'],//资金面及预期
                'emotion_content' => $v['emotion_content'],//情绪面
                'expect_content' => $v['expect_content'],//市场预期/关注
                'news_content' => $v['news_content'],//市场新闻
                'content_time' => $content_time,//发布时间
                'file_img' => array_filter($file_img),//
                'file_img_num' => count(array_filter($file_img)),//图片数组
                'file_video' => array_filter($file_video),//
                'file_audio' => array_filter($file_audio),//
                'file_data' => array_filter($file_data),//
                'create_time' => date('Y-m-d H:i:s', $v['createtime']),//创建时间
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
                'is_show_file' => $is_show_file,//0=文字,1=展示文件
                'file_list' => $file_list,//文件列表
            ];

            $arList[] = $content_arr;
        }
        $res = [
            'investmentList' => $arList,
            'total' => $data['total']
        ];
        return $res;
    }


    //获取文章数据列表
    public function index()
    {
        $request = $this->request->param();//参数
        $category_type = $request['category_type'];//类型|plaza:广场帖子,plate:论坛帖子,news:新闻,video:视频,live:直播,investment:投研日志
        $is_get_all = $this->request->param('is_get_all', 0);//是否获取全部分类下的2级栏目/获取顶级分类首页|0:否,1:是
        $category_id = $request['category_id'];//分类id|fa_channels表id
        if (!$category_id) {
            $this->error('缺少分类id');
        }

        $page = $request['page'] ?? 1;//页码
        $limit = $request['limit'] ?? 20;//每页数量

        $keyword = $request['keyword'] ?? '';//关键词
        $time = $request['time'] ?? '';//时间筛选(格式:2023-09-13)
        $time = empty($time) ? date('Y-m-d') : $time;
        $time = $time . ' 23:59:59';

        //开始时间(格式:2023-10-13)
        $start_time = $this->request->param('start_time', '');
        //结束时间筛选(格式:2023-10-13)
        $end_time = $this->request->param('end_time', '');
        if ($start_time && $end_time) {
            $start_time = $start_time . ' 00:00:00';
            $end_time = $end_time . ' 23:59:59';
        } else {
            $start_time = '';
            $end_time = '';
        }

        $type = $request['type'] ?? 'all';//类型1:今日,2:七日内发布的内容,3:本月发布的,4:今年发布的
        $orderway = $request['orderway'] ?? 'desc';//排序方式:asc,desc

        $user = $this->auth->getUser();//用户信息
        $new_user = new User();
        $channel_id = $request['category_id'];//栏目表id|fa_channels表id
        $rule_column_id = $new_user->isChannelsPermission($user)['rule_column_id'];//当前用户拥有权限的的全部栏目id

        $channel = Channel::where('id', $category_id)->field('id,parent_id,correlation_id')->find();
        if (!$channel) {
            $this->error('当前分类不存在');
        }
        //判断传过来的是不是顶级分类id,是则获取该顶级id下的第一个二级id(注意:当前首页广场是没有二级分类的)
        if (!$channel['parent_id'] && !$channel['correlation_id']) {
            //根据顶级id获取他下面的全部子集id
            $category_id = Channel::where('parent_id', $category_id)->order('correlation_id', 'asc')->value('correlation_id');
        } else {
            $category_id = $channel['correlation_id'];
        }

        switch ($category_type) {
            case 'plaza':
                $this->getPlazaList($user, $page, $limit, $keyword, $start_time, $end_time, $type, $orderway, $rule_column_id);
                break;
            case 'plate':
                $this->getPlateList($user, $category_id, $page, $limit, $keyword, $start_time, $end_time, $type, $orderway, $channel_id, $channel, $rule_column_id);
                break;
            case 'news':
                $this->getNewsList($user, $category_id, $page, $limit, $keyword, $start_time, $end_time, $type, $orderway, $channel_id, $rule_column_id);
                break;
            case 'video':
                $this->getVideoList($user, $category_id, $page, $limit, $keyword, $time, $type, $orderway, $channel_id, $rule_column_id);
                break;
            case 'live':
                $this->getLiveList($user, $category_id, $page, $limit, $keyword, $time, $type, $orderway, $channel_id, $rule_column_id);
                break;
            case 'investment':
                $this->getInvestmentLists($user, $category_id, $page, $limit, $keyword, $time, $start_time, $end_time, $orderway, $channel_id, $rule_column_id, $is_get_all);
                break;
            case 'chart':
                $this->getChartList($user, $category_id, $page, $limit, $channel_id, $rule_column_id, $time, $end_time);
                break;
            default:
                $this->error('类型不正确');
        }
    }

    //获取广场文章列表(全部二级分类的内容)
    private function getPlazaList($user, $page, $limit, $keyword, $start_time, $end_time, $type, $orderway, $rule_column_id)
    {
        //判断是否开通分类会员
        $membershipExpiryRecord = new MembershipExpiryRecord();
        $plate_pay_category_ids = Plate::where('membership_price', '>', 0)->where('status', 1)->column('id');//收费的自有栏目id

        //获取论坛文章表中的全部二级分类文章
        $plate_post = Platepost::where('is_audit', 1);
        if ($start_time && $end_time) {
            $plate_post = $plate_post->whereTime('creat_time', 'between', [$start_time, $end_time]);
        }
        $plate_post = $plate_post->with(['user', 'category' => function (Query $query) {
            $query->where('status', 1);
        }])
            ->order('id', $orderway)
            ->paginate($limit, false, ['page' => $page])
            ->toArray();
        $platePostList = [];
        foreach ($plate_post['data'] as $v) {
            $commentData = Comment::where('pid', 0)
                ->where('aid', $v['id'])
                ->where('type', 'bbs')
                ->with(['user', 'children'])
                ->limit(5)->select();

            $commentList = [];
            foreach ($commentData as $ck => $cv) {
                $commentList[$ck] = $cv->toArray();
                $commentList[$ck]['createtime'] = date('Y-m-d H:i:s', $commentList[$ck]['createtime']);
                $commentList[$ck]['user'] = [];
                if (!$cv['user']) {
                    $cv['user'] = User::getSoftDeleteUser($cv['user_id']);
                }
                $commentList[$ck]['user'] = [
                    'id' => $cv['user']['id'] ?? '',
                    'username' => $cv['user']['nickname'] ?? '',
                    'avatar' => obsurl($cv['user']['avatar'] ?? ''),
                ];
                $commentList[$ck]['is_children'] = $cv['children'] ? 1 : 0;//是否存在子集|0:否,1:是
                $commentList[$ck]['is_author'] = 0;//是否为评论作者|1:是,0:否
                if ($commentList[$ck]['user']['id'] == $user['id']) {
                    $commentList[$ck]['is_author'] = 1;//是否为评论作者|1:是,0:否
                }
            }
            $is_mylike = UserLike::where('table_name', 'plate_post')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->find();
            $is_myfavorite = UserFavorite::where('table_name', 'plate_post')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->find();

            if (!$v['user']) {
                $v['user'] = User::getSoftDeleteUser($v['user_id']);
            }

            if ($v['user']['avatar'] ?? null) {
                $v['user']['avatar'] = obsurl($v['user']['avatar']);
            }
            if ($v['file']) {
                $v['file'] = obsurl($v['file']);
            }
            $channel_id = Channel::where('correlation_id', $v['plateid'])
                ->where('parent_id', 3)
                ->value('id');

            $file_img = [];
            foreach (explode(',', $v['file_img']) as $path) {
                $file_img[] = obsurl($path);
            }
            $file_video = [];
            foreach (explode(',', $v['file_video']) as $path) {
                $file_video[] = obsurl($path);
            }
            $file_audio = [];
            foreach (explode(',', $v['file_audio']) as $path) {
                $file_audio[] = obsurl($path);
            }
            $file_data = [];
            if ($v['file_data']) {
                $attachment = Attachment::whereIn('url', $v['file_data'])->select();
                foreach ($attachment as $av) {
                    $file_data[] = [
                        'filename' => $av['filename'],
                        'url' => obsurl($av['url'])
                    ];
                }
            }

            $reprint_data = [];//转发的原文信息
            if ($v['reprint_id']) {
                $reprint_article = Platepost::find($v['reprint_id']);//被转发文原文

                $r_file_img = [];
                foreach (explode(',', $reprint_article['file_img']) as $path) {
                    $r_file_img[] = obsurl($path);
                }
                $r_file_video = [];
                foreach (explode(',', $reprint_article['file_video']) as $path) {
                    $r_file_video[] = obsurl($path);
                }
                $r_file_audio = [];
                foreach (explode(',', $reprint_article['file_audio']) as $path) {
                    $r_file_audio[] = obsurl($path);
                }
                $r_file_data = [];
                if ($reprint_article['file_data']) {
                    $r_attachment = Attachment::whereIn('url', $reprint_article['file_data'])->select();
                    foreach ($r_attachment as $r_av) {
                        $r_file_data[] = [
                            'filename' => '复盘-' . $r_av['filename'],
                            'url' => obsurl($r_av['url'])
                        ];
                    }
                }

                $reprint_data = [
                    'category_type' => 'plate',//类型|plaza:广场帖子,plate:论坛帖子,news:新闻,video:视频,live:直播,investment:投研日志
                    'comment_type' => 'bbs',//news:新闻,bbs:论坛,video:视频,plaza:广场,live:直播
                    'id' => $reprint_article['id'],
                    'post_title' => $reprint_article['postname'],//标题
                    'post_content' => $reprint_article['postcontent'],//内容
                    'post_content_text' => filteredText($reprint_article['postcontent']),//内容
                    'file_img' => array_filter($r_file_img),//图片数组
                    'file_img_num' => count(array_filter($r_file_img)),//图片数组
                    'file_video' => array_filter($r_file_video),//视频数组
                    'file_audio' => array_filter($r_file_audio),//音频数组
                    'file_data' => array_filter($r_file_data),//文件数组
                ];
            }


            $data = [
                'channel_id' => $channel_id,//栏目表id
                'table_name' => 'plate_post',//内容以前所在表,不带前缀|news:新闻,plaza_post:广场,video:视频,plate_post:论坛
                'category_type' => 'plate',//类型|plaza:广场帖子,plate:论坛帖子,news:新闻,video:视频,live:直播,investment:投研日志
                'comment_type' => 'bbs',//news:新闻,bbs:论坛,video:视频,plaza:广场,live:直播
                'id' => $v['id'],
                'post_title' => $v['postname'],//标题
                'post_content' => $v['postcontent'],//内容
                'post_content_text' => filteredText($v['postcontent']),//内容

                'membership_price' => $v['membership_price'],//会员价格|0:免费
                'user_id' => $v['user_id'],//用户id
                'avatar' => $v['user']['avatar'] ?? '',//用户头像
                'username' => $v['user']['nickname'] ?? '',//昵称
                'thumbnail' => $v['file'],//图片链接
                'is_accessory' => ($v['accessory'] ? 1 : 0),//是否存在附件|0:否,1:是
                'is_top' => $v['top'],//是否置顶|0:否
                'is_mylike' => $is_mylike ? 1 : 0,//是否喜欢|0:否
                'is_myfavorite' => $is_myfavorite ? 1 : 0,//是否收藏|0:否
                'recommended' => $v['is_jing'],//是否推荐|0:否
                'commentList' => $commentList,//顶级评论列表

                'create_time' => $v['creat_time'],//创建时间
                'post_like' => $v['post_like'],//点赞数量
                'post_favorites' => $v['post_favorites'] ?? 0,//收藏数量
                'comment_count' => $v['comment_count'],//评论数量
                'post_forward' => $v['post_forward'],//转发数量
                'post_hits' => 0,//点击量,

                'is_author' => 0,//是否为作者|0:否,1:是

                'file_img' => array_filter($file_img),//图片数组
                'file_img_num' => count(array_filter($file_img)),//图片数组
                'file_video' => array_filter($file_video),//视频数组
                'file_audio' => array_filter($file_audio),//音数数组
                'file_data' => array_filter($file_data),//文件数组
                'is_reprint' => $this->is_reprint($v['postcontent']),//判断文章内容是否是转载 1:是,0:否
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
                'reprint_id' => $v['reprint_id'],//转发的原文id|值>=1=转发,值不为真=原创
                'reprint_data' => $reprint_data,//转发的原文数据
            ];
            if ($user['id'] == $v['user_id']) {
                $data['is_author'] = 1;
            }

            //判断栏目分类是不是在用户拥有的栏目权限表中,不在就验证用户有无购买自有栏目,也不在就验证用户有没有单独购买文章
            if (!in_array(18, $rule_column_id) || !in_array(20, $rule_column_id)) {
                //判断分类是否收费
                if (in_array($v['plateid'], $plate_pay_category_ids)) {
                    //判断用户是否购买该分类
                    $isCategoryMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'categories', 'plate', $v['plateid']);
                    if ($isCategoryMemberExpire['code'] != 200) {
                        //用户没买分类
//                        $data['post_content'] = '';
//                        $data['post_title'] = '';
//                        $data['file_img'] = [];
//                        $data['file_video'] = [];
//                        $data['file_data'] = [];

                        $data['is_member_expire'] = 1;//会员是否到期|0:否,1:是
                    }
                } elseif ($v['membership_price'] >= 0.1) {
                    //判断用户是否购内容
                    $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'articles', 'plate_post', $v['id']);
                    if ($isMemberExpire['code'] != 200) {
                        //用户没买内容
//                        $data['post_content'] = '';
//                        $data['post_title'] = '';
                        $data['is_member_expire'] = 1;//会员是否到期|0:否,1:是
//                        $data['file_img'] = [];
//                        $data['file_video'] = [];
//                        $data['file_data'] = [];
                    }
                }
            }

            $platePostList[] = $data;
        }

        $res = [
            'total' => $plate_post['total'],
            'platePostList' => $platePostList,
        ];
        $this->success('', $res);
    }

    //获取论坛文章列表
    private function getPlateList($user, $category_id, $page, $limit, $keyword, $start_time, $end_time, $type, $orderway, $channel_id, $channel, $rule_column_id, $is_get_all = 0)
    {
        $category = Plate::where('id', $category_id)->find();
        //判断是否开通分类会员
        $membershipExpiryRecord = new MembershipExpiryRecord();
        $is_fee = 0;//是否收费|0:否,1:是
        $is_member = 1;//是否购买了会员组|0:否,1:是
        //判断用户会员组权限是否没有当前栏目的权限
        if (!in_array($channel_id, $rule_column_id)) {
            $is_member = 0;
            //没权限就判断用户是否购买了需要付费的栏目
            if ((float)($category['membership_price'] ?? 0) >= 0.01) {
                $is_fee = 1;
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'categories', 'plate', $category_id);
                if ($isMemberExpire['code'] != 200) {
                    $this->error($isMemberExpire['msg']);
                }
            }
        }

        //没查出来分类就拿全部文章数据
        if (!$category) {
            $data = Platepost::where('is_audit', '1');
        } else {
            $data = Platepost::where('plateid', $category_id)->where('is_audit', '1');
        }

        if ($keyword) {
            $data = $data->where('postname', 'like', "%{$keyword}%");
        }

        if ($start_time && $end_time) {
            $data = $data->whereTime('creat_time', 'between', [$start_time, $end_time]);
        }


        if ($type != 'all') {
            $getStartAndEndTime = $this->getStartAndEndTime($type);//根据类型获取日期时间
            $data = $data->where('creat_time', '>=', $getStartAndEndTime['start_time']);
        }

        $data = $data->with(['user'])->order('top', 'desc')->order('is_jing', 'desc')->order('id', $orderway)->paginate($limit, false, ['page' => $page])->toArray();


        //附件列表
        $attachment_url_arr = [];
        foreach ($data['data'] as $v) {
            if ($v['file_data']) {
                $attachment_url_arr = array_merge($attachment_url_arr, explode(',', $v['file_data']));
            }
        }
        //获取附件列表
        $attachment_list = (new AttachmentService())->getList(['url' => array_unique(array_filter($attachment_url_arr))]);

        $arList = [];
        foreach ($data['data'] as $v) {
            $commentData = Comment::where('pid', 0)->where('aid', $v['id'])
                ->where('type', 'bbs')
                ->with(['user', 'children'])
                ->limit(5)->select();

            $commentList = [];
            foreach ($commentData as $ck => $cv) {
                $commentList[$ck] = $cv->toArray();
                $commentList[$ck]['createtime'] = date('Y-m-d H:i:s', $commentList[$ck]['createtime']);
                if (!$cv['user']) {
                    $cv['user'] = User::getSoftDeleteUser($cv['user_id']);
                }
                $commentList[$ck]['user'] = [
                    'id' => $cv['user']['id'] ?? '',
                    'username' => $cv['user']['nickname'] ?? '',
                    'avatar' => obsurl($cv['user']['avatar'] ?? ''),
                ];
                $commentList[$ck]['is_children'] = $cv['children'] ? 1 : 0;//是否存在子集|0:否,1:是
                $commentList[$ck]['is_author'] = 0;//是否为评论作者|1:是,0:否
                if ($commentList[$ck]['user']['id'] == $user['id']) {
                    $commentList[$ck]['is_author'] = 1;//是否为评论作者|1:是,0:否
                }
            }
            $is_mylike = UserLike::where('table_name', 'plate_post')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->find();
            $is_myfavorite = UserFavorite::where('table_name', 'plate_post')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->find();

            if (!$v['user']) {
                $v['user'] = User::getSoftDeleteUser($v['user_id']);
            }

            if ($v['user']['avatar'] ?? '') {
                $v['user']['avatar'] = obsurl($v['user']['avatar']);
            }
            if ($v['file']) {
                $v['file'] = obsurl($v['file']);
            }

            $file_img = [];
            foreach (explode(',', $v['file_img']) as $path) {
                $file_img[] = obsurl($path);
            }
            $file_video = [];
            foreach (explode(',', $v['file_video']) as $path) {
                $file_video[] = obsurl($path);
            }
            $file_audio = [];
            foreach (explode(',', $v['file_audio']) as $path) {
                $file_audio[] = obsurl($path);
            }
            $file_data = [];
            if ($v['file_data']) {
                $v['file_data'] = explode(',', $v['file_data']);
                foreach($v['file_data'] as $fv){
                    foreach ($attachment_list as $av) {
                        if ($av['url'] == $fv){
                            $file_data[] = [
                                'filename' => $av['filename'],
                                'url' => obsurl($av['url'])
                            ];
                        }
                    }
                }
            }

            $reprint_data = [];//转发的原文信息
            if ($v['reprint_id']) {
                $reprint_article = Platepost::find($v['reprint_id']);//被转发文原文

                $r_file_img = [];
                foreach (explode(',', $reprint_article['file_img']) as $path) {
                    $r_file_img[] = obsurl($path);
                }
                $r_file_video = [];
                foreach (explode(',', $reprint_article['file_video']) as $path) {
                    $r_file_video[] = obsurl($path);
                }
                $r_file_audio = [];
                foreach (explode(',', $reprint_article['file_audio']) as $path) {
                    $r_file_audio[] = obsurl($path);
                }
                $r_file_data = [];
                if ($reprint_article['file_data']) {
                    $r_attachment = Attachment::whereIn('url', $reprint_article['file_data'])->select();
                    foreach ($r_attachment as $r_av) {
                        $r_file_data[] = [
                            'filename' => '复盘-' . $r_av['filename'],
                            'url' => obsurl($r_av['url'])
                        ];
                    }
                }

                $reprint_data = [
                    'category_type' => 'plate',//查看详情用|类型|plaza:广场帖子,plate:论坛帖子,news:新闻,video:视频,live:直播,investment:投研日志
                    'comment_type' => 'bbs',//发布评论用|news:新闻,bbs:论坛,video:视频,plaza:广场,live:直播
                    'id' => $reprint_article['id'],
                    'post_title' => $reprint_article['postname'],//标题
                    'post_content' => $reprint_article['postcontent'],//内容
                    'post_content_text' => filteredText($reprint_article['postcontent']),//内容
                    'file_img' => array_filter($r_file_img),//图片数组
                    'file_img_num' => count(array_filter($r_file_img)),//图片数组
                    'file_video' => array_filter($r_file_video),//视频数组
                    'file_audio' => array_filter($r_file_audio),//音频数组
                    'file_data' => array_filter($r_file_data),//文件数组
                ];
            }


            $content_arr = [
                //1级分类id
                'channel_pid' => $channel['parent_id'],//栏目表父级分类id|0:顶级
                'channel_id' => $channel['id'],//栏目表2级id
                'table_name' => 'plate_post',//点赞收藏用|内容以前所在表,不带前缀|news:新闻,plaza_post:广场,video:视频,plate_post:论坛
                'category_type' => 'plate',//查看详情用|类型|plaza:广场帖子,plate:论坛帖子,news:新闻,video:视频,live:直播,investment:投研日志
                'comment_type' => 'bbs',//发布评论用|news:新闻,bbs:论坛,video:视频,plaza:广场,live:直播
                'id' => $v['id'],
                'post_title' => $v['postname'],//标题
                'post_content' => $v['postcontent'],//内容
                'post_content_text' => filteredText($v['postcontent']),//内容
                'membership_price' => $v['membership_price'],//会员价格|0:免费
                'user_id' => $v['user_id'],//用户id
                'avatar' => $v['user']['avatar'] ?? '',//用户头像
                'username' => $v['user']['nickname'] ?? '',//昵称
                'thumbnail' => $v['file'],//图片链接
                'is_accessory' => ($v['accessory'] ? 1 : 0),//是否存在附件|0:否,1:是
                'is_top' => $v['top'],//是否置顶|0:否
                'is_jing' => $v['is_jing'],//是否加精|0:否
                'is_mylike' => $is_mylike ? 1 : 0,//是否喜欢|0:否
                'is_myfavorite' => $is_myfavorite ? 1 : 0,//是否收藏|0:否
                'recommended' => $v['is_jing'],//是否推荐|0:否
                'commentList' => $commentList,//顶级评论列表
                'create_time' => (!$v['creat_time'] ? '':date('Y-m-d H:i',strtotime($v['creat_time']))),//创建时间

                'post_like' => $v['post_like'],//点赞数量
                'post_favorites' => $v['post_favorites'] ?? 0,//收藏数量
                'comment_count' => $v['comment_count'],//评论数量
                'post_hits' => 0,//点击量
                'post_forward' => $v['post_forward'],//转发数量

                'is_author' => 0,//是否为作者|0:否,1:是

                'file_img' => array_filter($file_img),//图片数组
                'file_img_num' => count(array_filter($file_img)),//图片数组
                'file_video' => array_filter($file_video),//视频数组
                'file_audio' => array_filter($file_audio),//音数数组
                'file_data' => array_filter($file_data),//文件数组
                'is_reprint' => $this->is_reprint($v['postcontent']),//判断文章内容是否是转载 1:是,0:否
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
                'reprint_id' => $v['reprint_id'],//转发的原文id|值>=1=转发,值不为真=原创
                'reprint_data' => $reprint_data,//转发的原文数据
            ];

            if ($user['id'] == $v['user_id']) {
                $content_arr['is_author'] = 1;
            }
            if ($is_fee == 0 && $is_member == 0) {
                //判断内容是否收费
                if ((float)$v['membership_price'] >= 0.01) {
                    //内容收费
                    $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'articles', 'plate_post', $v['id']);
                    //判断开没开内容会员
                    if ($isMemberExpire['code'] != 200) {
                        //没开内容会员
//                        $content_arr['post_title'] = '';
//                        $content_arr['post_content'] = '';
                        $content_arr['is_member_expire'] = 1;//会员是否到期|0:否,1:是
                    }
                }
            }
            $arList[] = $content_arr;
        }
        $res = [
            'plateList' => $arList,
            'total' => $data['total']
        ];
        $this->success('', $res);
    }

    //获取新闻资讯列表
    private function getNewsList($user, $category_id, $page, $limit, $keyword, $start_time, $end_time, $type, $orderway, $channel_id, $rule_column_id)
    {
        $category = NewsCategoryModel::where('id', $category_id)->find();

        //判断是否开通分类会员
        $membershipExpiryRecord = new MembershipExpiryRecord();
        $is_fee = 0;//是否收费|0:否,1:是
        $is_member = 1;//是否购买了会员组|0:否,1:是
//判断用户会员组权限是否没有当前栏目的权限
        if (!in_array($channel_id, $rule_column_id)) {
            $is_member = 0;
            //没权限就判断用户是否购买了需要付费的栏目
            if ((float)($category['membership_price'] ?? 0) >= 0.01) {
                $is_fee = 1;
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'categories', 'news_category', $category_id);
                if ($isMemberExpire['code'] != 200) {
                    $this->error($isMemberExpire['msg']);
                }
            }
        }

        //没查出来分类就拿全部文章数据
        if (!$category) {
            $data = News::where('post_status', '1')->where('status', '1');
        } else {
            $data = News::where('parent_id', $category_id)->where('post_status', '1')->where('status', '1');
        }

        if ($keyword) {
            $data->where('post_content', 'like', "%{$keyword}%");
        }

        if ($start_time && $end_time) {
            $data = $data->whereTime('published_time', 'between', [$start_time, $end_time]);
        }

        if ($type != 'all') {
            $getStartAndEndTime = $this->getStartAndEndTime($type);//根据类型获取日期时间
            $data->where('published_time', '>=', strtotime($getStartAndEndTime['start_time']));
        }

        $data = $data->with(['user'])->order('id', $orderway)->paginate($limit, false, ['page' => $page])->toArray();

        $arList = [];
        foreach ($data['data'] as $v) {
            $commentData = Comment::where('pid', 0)->where('aid', $v['id'])
                ->where('type', 'news')
                ->with(['user', 'children'])
                ->limit(5)->select();

            $commentList = [];
            foreach ($commentData as $ck => $cv) {
                $commentList[$ck] = $cv->toArray();
                $commentList[$ck]['createtime'] = date('Y-m-d H:i:s', $commentList[$ck]['createtime']);
                if (!$cv['user']) {
                    $cv['user'] = User::getSoftDeleteUser($cv['user_id']);
                }
                $commentList[$ck]['user'] = [
                    'id' => $cv['user']['id'] ?? '',
                    'username' => $cv['user']['nickname'] ?? '',
                    'avatar' => $cv['user']['avatar'] ?? '',
                ];
                $commentList[$ck]['is_children'] = $cv['children'] ? 1 : 0;//是否存在子集|0:否,1:是
                $commentList[$ck]['is_author'] = 0;//是否为评论作者|1:是,0:否
                if ($commentList[$ck]['user']['id'] == $user['id']) {
                    $commentList[$ck]['is_author'] = 1;//是否为评论作者|1:是,0:否
                }
            }

            $is_mylike = UserLike::where('table_name', 'news')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->find();
            $is_myfavorite = UserFavorite::where('table_name', 'news')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->find();

            if (!$v['user']) {
                $v['user'] = User::getSoftDeleteUser($v['user_id']);
            }

            if ($v['user']['avatar'] ?? '') {
                if (substr($v['user']['avatar'], 0, 1) === '/') {
                    $v['user']['avatar'] = obsurl($v['user']['avatar']);
                }
            }
            if ($v['thumbnail']) {
                if (substr($v['thumbnail'], 0, 1) === '/') {
                    $v['thumbnail'] = obsurl($v['thumbnail']);
                }
            }

            $file_img = [];
            foreach (explode(',', $v['file_img']) as $path) {
                $file_img[] = obsurl($path);
            }
            $file_video = [];
            foreach (explode(',', $v['file_video']) as $path) {
                $file_video[] = obsurl($path);
            }
            $file_audio = [];
            foreach (explode(',', $v['file_audio']) as $path) {
                $file_audio[] = obsurl($path);
            }
            $file_data = [];
            if ($v['file_data']) {
                $attachment = Attachment::whereIn('url', $v['file_data'])->select();
                foreach ($attachment as $av) {
                    $file_data[] = [
                        'filename' => $av['filename'],
                        'url' => obsurl($av['url'])
                    ];
                }
            }

            //内容数组
            $content_arr = [
                'id' => $v['id'],
                'post_title' => $v['post_title'],//标题
                'post_content' => $v['post_content'],//内容
                'post_content_text' => filteredText($v['post_content']),//内容
                'membership_price' => $v['membership_price'],//会员价格|0:免费
                'user_id' => $v['user_id'],//用户id
                'thumbnail' => $v['thumbnail'],//缩略图
                'avatar' => $v['user']['avatar'] ?? '',//头像
                'username' => $v['user']['nickname'] ?? '',//昵称
                'is_top' => $v['is_top'],//是否置顶|0:否
                'is_mylike' => $is_mylike ? 1 : 0,//是否喜欢|0:否
                'is_myfavorite' => $is_myfavorite ? 1 : 0,//是否收藏|0:否
                'recommended' => $v['recommended'],//是否推荐|0:否
                'commentList' => $commentList,//顶级评论列表

                'post_like' => $v['post_like'],//点赞数量
                'post_favorites' => $v['post_favorites'] ?? 0,//收藏数量
                'comment_count' => $v['comment_count'] ?? 0,//评论数量
                'post_hits' => 0,//点击量
                'post_forward' => $v['post_forward'] ?? 0,//转发数量

                'create_time' => date('Y-m-d H:i:s', $v['published_time']),//创建时间
                'is_author' => 0,//是否为作者|0:否,1:是

                'file_img' => array_filter($file_img),//图片数组
                'file_img_num' => count(array_filter($file_img)),//图片数组
                'file_video' => array_filter($file_video),//视频数组
                'file_audio' => array_filter($file_audio),//音数数组
                'file_data' => array_filter($file_data),//文件数组
                'is_reprint' => $this->is_reprint($v['post_content']),//判断文章内容是否是转载 1:是,0:否
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
            ];
            if ($user['id'] == $v['user_id']) {
                $content_arr['is_author'] = 1;
            }
            //分类免费
            if ($is_fee == 0 && $is_member == 0) {
                //判断内容是否收费
                if ((float)$v['membership_price'] >= 0.01) {
                    //内容收费
                    $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'articles', 'news', $v['id']);
                    //判断开没开内容会员
                    if ($isMemberExpire['code'] != 200) {
                        //没开内容会员
//                        $content_arr['post_title'] = '';
//                        $content_arr['post_content'] = '';
                        $content_arr['is_member_expire'] = 1;//会员是否到期|0:否,1:是
                    }
                }
            }
            $arList[] = $content_arr;
        }
        $res = [
            'newsList' => $arList,
            'total' => $data['total']
        ];
        $this->success('', $res);
    }

    //获取视频列表
    private function getVideoList($user, $category_id, $page, $limit, $keyword, $time, $type, $orderway, $channel_id, $rule_column_id)
    {
        $category = VideoCategoryModel::where('id', $category_id)->find();

        $membershipExpiryRecord = new MembershipExpiryRecord();
        $is_fee = 0;//是否收费|0:否,1:是
        $is_member = 1;//是否购买了会员组|0:否,1:是
//判断用户会员组权限是否没有当前栏目的权限
        if (!in_array($channel_id, $rule_column_id)) {
            $is_member = 0;
            //没权限就判断用户是否购买了需要付费的栏目
            if ((float)($category['membership_price'] ?? 0) >= 0.01) {
                $is_fee = 1;
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'categories', 'video_category', $category_id);
                if ($isMemberExpire['code'] != 200) {
                    $this->error($isMemberExpire['msg']);
                }
            }
        }


        //没查出来分类就拿全部文章数据
        if (!$category) {
            $data = new Video();
        } else {
            $data = Video::where('video_category_id', $category_id);
        }

        if ($keyword) {
            $data->where('title', 'like', "%{$keyword}%");
        }

        if ($time) {
            $data->where('create_time', '<=', strtotime($time));
        }

        if ($type != 'all') {
            $getStartAndEndTime = $this->getStartAndEndTime($type);//根据类型获取日期时间
            $data->where('create_time', '>=', strtotime($getStartAndEndTime['start_time']));
        }

        $data = $data->with(['user'])->order('is_top', 'desc')->order('sort', 'desc')->order('id', $orderway)->paginate($limit, false, ['page' => $page])->toArray();

        $arList = [];
        foreach ($data['data'] as $v) {
            $commentData = Comment::where('pid', 0)->where('aid', $v['id'])
                ->where('type', 'video')
                ->with(['user', 'children'])
                ->limit(5)->select();

            $commentList = [];
            foreach ($commentData as $ck => $cv) {
                $commentList[$ck] = $cv->toArray();
                $commentList[$ck]['createtime'] = date('Y-m-d H:i:s', $commentList[$ck]['createtime']);
                if (!$cv['user']) {
                    $cv['user'] = User::getSoftDeleteUser($cv['user_id']);
                }
                $commentList[$ck]['user'] = [
                    'id' => $cv['user']['id'],
                    'username' => $cv['user']['nickname'],
                    'avatar' => $cv['user']['avatar'],
                ];
                $commentList[$ck]['is_children'] = $cv['children'] ? 1 : 0;//是否存在子集|0:否,1:是
                $commentList[$ck]['is_author'] = 0;//是否为评论作者|1:是,0:否
                if ($commentList[$ck]['user']['id'] == $user['id']) {
                    $commentList[$ck]['is_author'] = 1;//是否为评论作者|1:是,0:否
                }
            }

            $is_mylike = UserLike::where('table_name', 'video')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->find();
            $is_myfavorite = UserFavorite::where('table_name', 'video')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->find();

            if ($v['image']) {
                if (substr($v['image'], 0, 1) === '/') {
                    $v['image'] = obsurl($v['image']);
                }
            }
            if ($v['video_path']) {
                if (substr($v['video_path'], 0, 1) === '/') {
                    $v['video_path'] = obsurl($v['video_path']);
                }
            }

            if (!$v['user']) {
                $v['user'] = User::getSoftDeleteUser($v['user_id']);
            }

            if ($v['user']['avatar']) {
                $v['user']['avatar'] = obsurl($v['user']['avatar']);
            }

            //内容数组
            $content_arr = [
                'id' => $v['id'],
                'title' => $v['title'],//标题
                'description' => $v['description'],//描述
                'image' => $v['image'],//封面图片
                'video_path' => $v['video_path'],//视频链接
                'is_top' => $v['is_top'],//是否置顶|0:否
                'type' => $v['type'],//类型：1视频2音频
                'is_mylike' => $is_mylike ? 1 : 0,//是否喜欢|0:否
                'is_myfavorite' => $is_myfavorite ? 1 : 0,//是否收藏|0:否
                'user_id' => $v['user_id'],//用户id
                'thumbnail' => $v['user']['avatar'],//头像
                'username' => $v['user']['nickname'],//昵称
                'commentList' => $commentList,//顶级评论列表
                'create_time' => $v['create_time'],//创建时间

                'post_like' => $v['count_like'] ?? 0,//点赞数量
                'post_hits' => $v['count_view'] ?? 0,//点击量
                'post_favorites' => $v['count_favorite'] ?? 0,//收藏数量
                'comment_count' => $v['count_comment'],//评论数量
                'post_forward' => $v['count_forward'] ?? 0,//转发数量

                'membership_price' => $v['membership_price'],//会员价格|0:免费
                'is_member_expire' => 0,//是否没有开通会员0:否,1:=是(没开内容会员)
            ];

            //分类免费 也没买会员组
            if ($is_fee == 0 && $is_member == 0) {
                //判断内容是否收费
                if ($v['membership_price'] > 0) {
                    //内容收费
                    $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'articles', 'video', $v['id']);
                    //判断开没开内容会员
                    if ($isMemberExpire['code'] != 200) {
                        //没开内容会员
                        $content_arr['description'] = '';
                        $content_arr['image'] = '';
                        $content_arr['video_path'] = '';
                        $content_arr['is_member_expire'] = 1;//是否没有开通会员0:否,1:=是(没开内容会员)
                    }
                }
            }
            $arList[] = $content_arr;
        }

        //获取专辑
        $albumList_model = AlbumModel::where('table_name', 'video_category')->where('category_id', $category_id)->order('sort', 'desc')->select();
        $album_count = count($albumList_model);
        foreach ($albumList_model as $v) {
            //内容数组
            $content_arr = [
                'id' => $v['id'],//专辑id
                'name' => $v['name'],//专辑名称
                'introduction' => $v['introduction'],//介绍
                'cover_image' => obsurl($v['cover_image']),//封面图url
                'table_name' => $v['table_name'],//分类类型|数据源不加前缀的表名|video_category=视频
                'category_id' => $v['category_id'],//所属分类id数据源不加前缀的表中的id|专辑归属的分类id
                'membership_price' => $v['membership_price'],//会员价格(元)|默认0:免费
                'membership_duration' => $v['membership_duration'],//会员天数|默认0天
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
                'quantity_sold' => OrderModel::where('table_name', 'album')->where('goods_id', $v['id'])->where('pay_status', 1)->count()//专辑购买数量
            ];
            //分类免费
            if ($is_fee == 0 && $is_member == 0) {
                //判断内容是否收费
                if ((float)$v['membership_price'] >= 0.01) {
                    //内容收费
                    $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'album', 'album', $v['id']);
                    //判断开没开内容会员
                    if ($isMemberExpire['code'] != 200) {
                        //没开内容会员
                        $content_arr['is_member_expire'] = 1;
                    }
                }
            }
            $albumList[] = $content_arr;
        }

        $res = [
            'total' => $data['total'],
            'videoList' => $arList,
            'albumList' => $albumList ?? [],//专辑列表
            'album_count' => $album_count ?? 0,//专辑数量
        ];
        $this->success('', $res);
    }

    //获取直播列表
    private function getLiveList($user, $category_id, $page, $limit, $keyword, $time, $type, $orderway, $channel_id, $rule_column_id)
    {
        $category = LiveCategoryModel::where('id', $category_id)->find();

        //判断是否开通分类会员
        $membershipExpiryRecord = new MembershipExpiryRecord();
        $is_fee = 0;//是否收费|0:否,1:是
        $is_member = 1;//是否购买了会员组|0:否,1:是
//判断用户会员组权限是否没有当前栏目的权限
        if (!in_array($channel_id, $rule_column_id)) {
            $is_member = 0;
            //没权限就判断用户是否购买了需要付费的栏目
            if ((float)($category['membership_price'] ?? 0) >= 0.01) {
                $is_fee = 1;
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'categories', 'live_category', $category_id);
                if ($isMemberExpire['code'] != 200) {
                    $this->error($isMemberExpire['msg']);
                }
            }
        }


        //没查出来分类就拿全部文章数据
        if (!$category) {
            $data = new Live();
        } else {
            $data = Live::where('live_category_id', $category_id);
        }

        if ($keyword) {
            $data->where('title', 'like', "%{$keyword}%");
        }

        if ($time) {
//            $data->where('live_time', '<=', strtotime($time));//开播时间
        }


        $data = $data->order('is_top', 'desc')->order('live_time', $orderway)->paginate($limit, false, ['page' => $page])->toArray();

        $arList = [];
        foreach ($data['data'] as $v) {
            $comment_count = Comment::favTotal($v['id'], 'live');
            //内容数组
            if ($v['image']) {
                if (substr($v['image'], 0, 1) === '/') {
                    $v['image'] = obsurl($v['image']);
                }
            }
            $content_arr = [
                'channel_id' => $channel_id,
                'id' => $v['id'],
                'title' => $v['title'],//标题
                'description' => $v['description'],//描述
                'image' => $v['image'],//封面图片
                'live_url' => $v['live_url'],//直播地址
                'live_time' => $v['live_time'],//直播时间
                'is_top' => $v['is_top'],//是否置顶|0:否
                'is_start_live' => $v['is_start_live'],//是否开始直播|0:否
                'create_time' => $v['create_time'],//创建时间
                'membership_price' => $v['membership_price'],//会员价格|0:免费
                'is_member_expire' => 0,//是否没有开通会员0:否,1:=是(没开内容会员)

//                'post_like' => $post_like,//点赞数量
//                'post_favorites' => $post_favorites,//收藏数量
                'comment_count' => $comment_count,//评论数量
//                'post_forward' => $post_forward,//转发数量
            ];

            //分类免费
            if ($is_fee == 0 && $is_member == 0) {
                //判断内容是否收费
                if ((float)$v['membership_price'] >= 0.01) {
                    //内容收费
                    $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'articles', 'live', $v['id']);
                    //判断开没开内容会员
                    if ($isMemberExpire['code'] != 200) {
                        //没开内容会员
                        $content_arr['description'] = '';
                        $content_arr['image'] = '';
                        $content_arr['live_url'] = '';
                        $content_arr['is_member_expire'] = 1;//会员是否到期|0:否,1:是
                    }
                }
            }
            $arList[] = $content_arr;
        }
        $res = [
            'liveList' => $arList,
            'total' => $data['total']
        ];
        $this->success('', $res);
    }

    /**
     * 获取投研日志(数据库)列表
     * @param $user
     * @param $category_id
     * @param $page
     * @param $limit
     * @param $keyword
     * @param $time
     * @param $orderway
     * @param int $is_get_home 是否要获取首页信息 0:指定分类信息正常获取,1:只获取每个分类前2条数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function getInvestmentLists($user, $category_id, $page, $limit, $keyword, $time, $start_time, $end_time, $orderway, $channel_id, $rule_column_id, $is_get_home = 0)
    {
        $category = InvestmentCategoryModel::where('id', $category_id)->find();
        //判断是否开通分类会员
        $membershipExpiryRecord = new MembershipExpiryRecord();
        $is_fee = 0;//是否收费|0:否,1:是
        $is_member = 1;//是否购买了会员组|0:否,1:是
//判断用户会员组权限是否没有当前栏目的权限
        if (!in_array($channel_id, $rule_column_id)) {
            $is_member = 0;
            //没权限就判断用户是否购买了需要付费的栏目
            if ((float)($category['membership_price'] ?? 0) >= 0.01) {
                $is_fee = 1;
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'categories', 'investment_category', $category_id);
                if ($isMemberExpire['code'] != 200) {
                    $this->error($isMemberExpire['msg']);
                }
            }
        }


        //获取首页列表
        if ($is_get_home) {
            $category_ids = InvestmentCategoryModel::column('id');
            $data['data'] = [];
            foreach ($category_ids as $v) {
                $model = Investment::where('investment_category_id', $v);
                if ($keyword) {
                    //查询分词索引表
                    $cut_word_index = new \app\api\service\cms\CutWordIndex();
                    $a_id = $cut_word_index->getCutWordIndex($keyword, 'investment')['a_id'];
                    if ($a_id) {
                        $model = $model->whereIn('id', $a_id);
                    }

                    //$model->where('macro_content', 'like', "%{$keyword}%");
                }
                if ($time) {
                    $model->where('createtime', '<=', strtotime($time));
                }
                if ($start_time && $end_time) {
                    $data->where('createtime', '>=', strtotime($start_time));
                    $data->where('createtime', '<=', strtotime($end_time));
                }
                $model = $model->order('id', 'desc')->limit(2)->select();
                $data['data'] = array_merge($data['data'], $model);
            }
            $data['total'] = count($data['data']);
        } else {
            //没查出来分类就拿全部文章数据
            if (!$category) {
                $data = new Investment();
            } else {
                $data = Investment::where('investment_category_id', $category_id);
            }
            if ($keyword) {
                //查询分词索引表
                $cut_word_index = new \app\api\service\cms\CutWordIndex();
                $a_id = $cut_word_index->getCutWordIndex($keyword, 'investment')['a_id'];
                if ($a_id) {
                    $data->whereIn('id', $a_id);
                }

                //$data->where('macro_content', 'like', "%{$keyword}%");
            }
            if ($time) {
                $data->where('createtime', '<=', strtotime($time));
            }

            if ($start_time && $end_time) {
                $data->where('createtime', '>=', strtotime($start_time));
                $data->where('createtime', '<=', strtotime($end_time));
            }

            $data = $data->order('createtime', $orderway)->paginate($limit, false, ['page' => $page])->toArray();
        }

        $arList = [];
        foreach ($data['data'] as $v) {

            //判断是否分类是只显示文件的
            $is_show_file = InvestmentCategoryModel::where('id', $v['investment_category_id'])->value('type');
            $file_list = [];
            if ($is_show_file == 1) {
                $file_list = json_decode($v['file_list'], true) ?? [];
                foreach ($file_list as &$fv) {
                    $fv['url'] = obsurl($fv['url']);
                }
            }

            $file_img = [];
            foreach (explode(',', $v['file_img']) as $path) {
                $file_img[] = obsurl($path);
            }
            $file_video = [];
            foreach (explode(',', $v['file_video']) as $path) {
                $file_video[] = obsurl($path);
            }
            $file_audio = [];
            foreach (explode(',', $v['file_audio']) as $path) {
                $file_audio[] = obsurl($path);
            }
            $file_data = [];
            if ($v['file_data']) {
                $attachment = Attachment::whereIn('url', $v['file_data'])->select();
                foreach ($attachment as $av) {
                    $file_data[] = [
                        'filename' => $av['filename'],
                        'url' => obsurl($av['url'])
                    ];
                }
            }

            //内容数组
            $content_arr = [
                'id' => $v['id'],
                'macro_content' => $v['macro_content'],//宏观面
                'policy_content' => $v['policy_content'],//政策面
                'fund_content' => $v['fund_content'],//资金面及预期
                'emotion_content' => $v['emotion_content'],//情绪面
                'expect_content' => $v['expect_content'],//市场预期/关注
                'news_content' => $v['news_content'],//市场新闻
                'file_img' => array_filter($file_img),//
                'file_img_num' => count(array_filter($file_img)),//图片数组
                'file_video' => array_filter($file_video),//
                'file_audio' => array_filter($file_audio),//
                'file_data' => array_filter($file_data),//
                'content_time' => date('Y-m-d H:i:s', $v['content_time']),//时间
                'create_time' => date('Y-m-d H:i:s', $v['createtime']),//创建时间
                'is_show_file' => $is_show_file,//0=不展示文件,1=展示文件
                'file_list' => $file_list,
            ];

            $arList[] = $content_arr;
        }
        $res = [
            'investmentList' => $arList,
            'total' => $data['total']
        ];
        $this->success('', $res);
    }

    //获取量化辅助(图表)列表
    private function getChartList($user, $category_id, $page, $limit, $channel_id, $rule_column_id, $start_time = null, $end_time = null)
    {
        //获取分类信息
        $category = Chart::where('id', $category_id)->find();

        //判断是否开通分类会员
        $membershipExpiryRecord = new MembershipExpiryRecord();
        $is_fee = 0;//是否收费|0:否,1:是
        $is_member = 1;//是否购买了会员组|0:否,1:是
//判断用户会员组权限是否没有当前栏目的权限
        if (!in_array($channel_id, $rule_column_id)) {
            $is_member = 0;
            //没权限就判断用户是否购买了需要付费的栏目
            if ((float)($category['membership_price'] ?? 0) >= 0.01) {
                $is_fee = 1;
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'categories', 'chart_parent', $category_id);
                if ($isMemberExpire['code'] != 200) {
                    $this->error($isMemberExpire['msg']);
                }
            }
        }

        //没查出来分类就拿全部文章数据
        if (!$category) {
            $data = new Diyform();
        } else {
            $data = Diyform::where('parent_id', $category_id);
        }
        //获取每张图表需要的字段
        $data = $data->order('id', 'desc')->paginate($limit, false, ['page' => $page])->toArray();
        //根据这些字段去对应的数据表中获取坐标
        $arList = [];
        foreach ($data['data'] as $v) {
            $fields = [];
            foreach ($v['fields'] as $fsv) {
                $fields = array_merge($fields, json_decode(trim($fsv, '[]'), true));
            }
            $field = [];
            foreach ($fields as $fields_k => $fields_v) {
                $field[] = $fields_k;
            }
            $field = array_merge($field, ['datetime']);

            //要是没有传递具体的时间则用fa_pro_diyform数据表常见的时间作为取值范围
            if (!$start_time || !$end_time) {
                $create_time = strtotime($v['createtime']);
                $end_time = date('Y-m-d', $create_time);
                $one_year_ago_timestamp = strtotime($end_time) - 365 * 24 * 60 * 60;
                $start_time = date('Y-m-d', $one_year_ago_timestamp);
            }
            //获取对用的图表坐标数据
            $chartInfo = Db::table($v['table'])->field($field)->select();
            $res_data = [];
            $datetime = [];
            //循环坐标数据,将相同字段的数据放在一起
            foreach ($chartInfo as $cv) {
                $datetime[] = date('Y-m-d', strtotime($cv['datetime']));
                foreach ($fields as $fk => $fv) {
                    $res_data[$fk][] = $cv[$fk];
                }
            }

            $chartInfo_data = [];
            $y_axial_2_data = [];//双Y轴数据
            //循环相同的坐标数据,将每个坐标数据进行充重组成新的数据给前端
            foreach ($res_data as $rk => $rv) {
                if ($rk == $v['y_axial_2']) {
                    $y_axial_2_data = [
                        'name' => $datetime,
                        'val' => $rv,
                        'line' => $fields[$rk],
                        'min_interval' => (float)$v['min_interval'],
                        'max_interval' => (float)$v['max_interval'],
                    ];
                } else {
                    $y_axial_2_data = [];
                }
                $chartInfo_data[] = [
                    'name' => $datetime,
                    'val' => $rv,
                    'line' => $fields[$rk],
                    'min_interval' => (float)$v['min_interval'],
                    'max_interval' => (float)$v['max_interval'],
                    'y_axial_2_data' => $y_axial_2_data,
                ];
            }

            //内容数组
            $content_arr = [
                'id' => $v['id'],
                'name' => $v['name'],
                'title' => $v['title'],
                'data' => $chartInfo_data,
                'membership_price' => $v['membership_price'],//会员价格|0:免费
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
            ];
            //分类免费
            if ($is_fee == 0 && $is_member == 0) {
                //判断内容是否收费
                if ((float)$v['membership_price'] >= 0.01) {
                    //内容收费
                    $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'articles', 'pro_diyform', $v['id']);
                    //判断开没开内容会员
                    if ($isMemberExpire['code'] != 200) {
                        //没开内容会员
                        $content_arr['data'] = '';
                        $content_arr['is_member_expire'] = 1;//会员是否到期|0:否,1:是
                    }
                }
            }
            $arList[] = $content_arr;
        }
        $res = [
            'chartList' => $arList,
            'total' => $data['total']
        ];
        $this->success('', $res);
    }

    //添加文章
    public function add()
    {
        $request = $this->request->param();//获取提交的参数
        $user = $this->auth->getUser();//用户信息

        $category_type = $request['category_type'] ?? '';//类型|plaza:广场帖子,plate:论坛帖子,news:新闻,如果没有且category_id也不为真则直接从config默认表中的广场快速发帖中获取category_id 和 category_type的值;
        $title = $request['title'] ?? null;//标题
        $file_img = rtrim(str_replace("\n", '', ($request['fileImgs'] ?? '')), ',');//图片,多个文件用英文逗号分割
        $file_video = rtrim(str_replace("\n", '', ($request['fileVideos'] ?? '')), ',');//视频,多个文件用英文逗号分割
        $file_audio = rtrim(str_replace("\n", '', ($request['fileMp3'] ?? '')), ',');//音频,多个文件用英文逗号分割
        $file_data = rtrim(str_replace("\n", '', ($request['file'] ?? '')), ',');//文件|

        $content = $request['content'] ?? '';//文章内容

        if (!$content && !$file_img && !$file_video && !$file_data && !$file_audio) {
            $this->error('内容/图片/视频/文件至少添加1个');
        }

        $category_id = $request['category_id'] ?? 0;//分类id(fa_channels表id)|如果没有且category_id也不为真则直接从config默认表中的广场快速发帖中获取category_id 和 category_type的值;

//        $rule = [
//            'content|内容' => 'require',
//        ];
//        $msg = [
//            'content.require' => '内容不能为空',
//        ];
//        $validate = new Validate($rule, $msg);
//        $result = $validate->check($request);
//        if (!$result) {
//            $this->error($validate->getError());
//        }
//
//        $content = $this->markdownToHtml($content);//Markdown 转 富文本html

        //如果没有且category_id也不为真则直接从config默认表中的广场快速发帖中获取category_id 和 category_type的值;
        if (!$category_type && !$category_id) {
            $category_id = \app\common\model\Config::where('name', 'plate_post_channels_id')->value('value');
            $category_type = 'plate';
        } elseif (!$category_id) {
            $this->error('缺少参数');
        }

        //检测-债市论坛-研究报告(fa_channels表id=108)-是否没传(文件)字段
        if ($category_type == 'plate' && $category_id == 108 && !$file_data){
            $this->error('请上传文件后再试');
        }


        $post_keywords = $request['post_keywords'] ?? null;//关键词|category_type=news用
        $post_excerpt = $request['post_excerpt'] ?? null;//post摘要|category_type=news用
        $post_source = $request['post_source'] ?? null;//转载文章的来源|category_type=news用

        $category_id = Channel::where('id', $category_id)->value('correlation_id');//转换成各自分类表的实际分类id

        switch ($category_type) {
            case 'plaza':
                $this->addPlaza($user, $category_id, $content, $title, null, $file_img, $file_video, $file_data);
                break;
            case 'plate':
                $this->addPlate($user, $category_id, $content, $title, null, $file_img, $file_video, $file_data, $file_audio);
                break;
            case 'news':
                $this->addNews($user, $category_id, $content, $title, $post_keywords, $post_excerpt, $post_source, null, $file_img, $file_video, $file_data);
                break;
            default:
                $this->error('类型不正确');
        }
    }

    //更新文章
    public function updatePost()
    {

        $id = $this->request->param('id', '');//文章id
        $category_type = $this->request->param('category_type', '');//文章类型|plaza:广场帖子,plate:论坛帖子
        $title = $this->request->param('title', '');//标题
        $file_img = rtrim(str_replace("\n", '', $this->request->param('fileImgs', '')), ',');//图片,多个文件用英文逗号分割
        $file_video = rtrim(str_replace("\n", '', $this->request->param('fileVideos', '')), ',');//视频,多个文件用英文逗号分割
        $file_audio = rtrim(str_replace("\n", '', ($request['fileMp3'] ?? '')), ',');//音频,多个文件用英文逗号分割
        $file_data = rtrim(str_replace("\n", '', $this->request->param('file', '')), ',');//文件|
        $content = $this->request->param('content', '');//文章内容

        if (!$content && !$file_img && !$file_video && !$file_data) {
            $this->error('内容/图片/视频/文件至少添加1个');
        }
        if ($file_img) {
            $file_img = explode(',', $file_img);
            $file_img = array_map(function ($url) {
                $parsedUrl = parse_url($url);
                return isset($parsedUrl['path']) ? $parsedUrl['path'] : $url;
            }, $file_img);
            $file_img = implode(',', $file_img);
        }

        if ($file_video) {
            $file_video = explode(',', $file_video);
            $file_video = array_map(function ($url) {
                $parsedUrl = parse_url($url);
                return isset($parsedUrl['path']) ? $parsedUrl['path'] : $url;
            }, $file_video);
            $file_video = implode(',', $file_video);
        }

        if ($file_audio) {
            $file_audio = explode(',', $file_audio);
            $file_audio = array_map(function ($url) {
                $parsedUrl = parse_url($url);
                return isset($parsedUrl['path']) ? $parsedUrl['path'] : $url;
            }, $file_audio);
            $file_audio = implode(',', $file_audio);
        }

        if ($file_data) {
            $file_data = explode(',', $file_data);
            $file_data = array_map(function ($url) {
                $parsedUrl = parse_url($url);
                return isset($parsedUrl['path']) ? $parsedUrl['path'] : $url;
            }, $file_data);
            $file_data = implode(',', $file_data);
        }

        // 正则表达式匹配网址格式
        $pattern = '/^(https?|ftp):\/\/[^\s\/$.?#].[^\s]*$/i';
        // 使用 preg_match 函数进行匹配
        $scraping_text_link = '';
        if (preg_match($pattern, $content) === 1) {
            $scraping_text_link = $content;
            $service = new FetchArticleService();
            $fetchArticle = $service->fetchArticle($scraping_text_link);
            if ($fetchArticle['code'] != 200) {
                $this->error('失败原因:' . $fetchArticle['msg']);
            }
            $title = $fetchArticle['title'];
            $content = $fetchArticle['html_content'];
            $obj = new CommonService();
            $content = $obj->contentFiles($content);//获取文章内容中的文件链接地址,将不是本项目的文件存到本项目中,并更新对应的文件地址
        }

        //$content = $this->markdownToHtml($content);//Markdown 转 富文本html @todo 前端富文本组件不用了 2024-02-21 17:13


        $user = $this->auth->getUser();//用户信息

        switch ($category_type) {
            case 'plaza'://广场
                $model = PlazaPostModel::where('id', $id)
                    ->where('user_id', $user['id'])
                    ->find();
                $model->postcontent = $content;
                $model->file_img = $file_img;
                $model->file_video = $file_video;
                $model->file_audio = $file_audio;
                $model->file_audio = $file_audio;
                $model->file_data = $file_data;
                $success = $model->save();
                break;
            case 'plate'://论坛
                $model = Platepost::where('id', $id)
                    ->where('user_id', $user['id'])
                    ->find();
                $model->postcontent = $content;
                $model->file_img = $file_img;
                $model->file_video = $file_video;
                $model->file_audio = $file_audio;
                $model->file_data = $file_data;
                $success = $model->save();

                //操作关键词索引表
                (new \app\api\service\cms\CutWordIndex())->updateCutWordIndex($id,'plate_post');
                break;
            default:
                $this->error('类型不正确');
        }
        if (!$success) $this->error('操作失败/内容无更新');
        $this->success("操作成功");

    }

    //删除文章
    public function delPost()
    {
        $id = $this->request->param('id', '');//文章id
        $category_type = $this->request->param('category_type', '');//文章类型|plaza:广场帖子,plate:论坛帖子
        $user = $this->auth->getUser();//用户信息

        switch ($category_type) {
            case 'plaza'://广场SoftDelete
                $model = PlazaPostModel::where('id', $id)
                    ->where('user_id', $user['id'])
                    ->find();
                break;
            case 'plate'://论坛
                $model = Platepost::where('id', $id)
                    ->where('user_id', $user['id'])
                    ->find();
                break;
            default:
                $this->error('类型不正确');
        }
        if (!$model) {
            $this->error('该内容已删除/不存在');
        }
        $model->deletetime = date('Y-m-d h:i:s');
        $success = $model->save();//软删除

        if (!$success) $this->error('操作失败');
        $this->success("操作成功");
    }

    //添加广场帖子
    private function addPlaza($user, $category_id, $content, $title, $reprint_id = null, $file_img = null, $file_video = null, $file_data = null)
    {
        // 正则表达式匹配网址格式
        $pattern = '/^(https?|ftp):\/\/[^\s\/$.?#].[^\s]*$/i';
        // 使用 preg_match 函数进行匹配
        $scraping_text_link = '';
        if (preg_match($pattern, $content) === 1) {
            $scraping_text_link = $content;
            $service = new FetchArticleService();//根据url爬去文章
            $fetchArticle = $service->fetchArticle($scraping_text_link);
            if ($fetchArticle['code'] != 200) {
                $this->error('失败原因:' . $fetchArticle['msg']);
            }
            $title = $fetchArticle['title'];
            $content = $fetchArticle['html_content'];
            $obj = new CommonService();
            $content = $obj->contentFiles($content);//获取文章内容中的文件链接地址,将不是本项目的文件存到本项目中,并更新对应的文件地址
        }

        //是否自动审核通过|0:否,1:是
        $is_audit = \app\common\model\Config::where('name', 'is_auto_approve_plaza')->value('value') ?? 0;//

        $success = PlazaPostModel::create([
            'scraping_text_link' => $scraping_text_link,
            'postname' => $title ?? null,
            'postcontent' => $content,
            'username' => $user['username'],
            'user_id' => $user['id'],
            'plaza_category_id' => $category_id,
            'is_audit' => $is_audit,
            'reprint_id' => $reprint_id,
            'file_img' => $file_img,
            'file_video' => $file_video,
            'file_data' => $file_data,
        ]);

        if (!$success) {
            $this->error("发生错误:" . '添加失败');
        }
        //自动加积分
        if ($is_audit == 1 && $success->id) {
            //增加积分
            $score = new Score();//调整积分
            $score->editScore($user['id'], $success->id, 'input_post_score', 1, 'plaza_post');
        }
        $this->success($is_audit ? "内容已发布" : "提交成功！请等待审核!");
    }

    //添加论坛帖子
    private function addPlate($user, $category_id, $content, $title, $reprint_id = null, $file_img = null, $file_video = null, $file_data = null, $file_audio = null)
    {
        // 正则表达式匹配网址格式
        $pattern = '/^(https?|ftp):\/\/[^\s\/$.?#].[^\s]*$/i';
        // 使用 preg_match 函数进行匹配
        $scraping_text_link = '';
        if (preg_match($pattern, $content) === 1) {
            $scraping_text_link = $content;
            $service = new FetchArticleService();
            $fetchArticle = $service->fetchArticle($scraping_text_link);
            if ($fetchArticle['code'] != 200) {
                $this->error('失败原因:' . $fetchArticle['msg']);
            }
            $title = $fetchArticle['title'];
            $content = $fetchArticle['html_content'];
            $obj = new CommonService();
            $content = $obj->contentFiles($content);//获取文章内容中的文件链接地址,将不是本项目的文件存到本项目中,并更新对应的文件地址
        }

        $is_audit = \app\common\model\Config::where('name', 'is_auto_approve_plate')->value('value') ?? 0;//
        $success = Platepost::create([
            'scraping_text_link' => $scraping_text_link,
            'postname' => $title ?? null,
            'plateid' => $category_id,
            'user_id' => $user['id'],
            'username' => $user['username'],
            'creat_time' => date('Y-m-d H:i:s'),
            'postcontent' => $content,
            'is_audit' => $is_audit,
            'reprint_id' => $reprint_id,
            'file_img' => $file_img,
            'file_video' => $file_video,
            'file_audio' => $file_audio,
            'file_data' => $file_data,
        ]);

        if (!$success) {
            $this->error("发生错误:" . '添加失败');
        }

        //操作关键词索引表
        (new \app\api\service\cms\CutWordIndex())->addCutWordIndex($success['id'],'plate_post');

        //自动加积分
        if ($is_audit == 1 && $success->id) {
            //增加积分
            $score = new Score();//调整积分
            $score->editScore($user['id'], $success->id, 'input_post_score', 1, 'plate_post');
        }
        $this->success($is_audit ? "内容已发布" : "提交成功！请等待审核!");
    }

    //添加新闻帖子
    private function addNews($user, $category_id, $content, $title, $post_keywords, $post_excerpt, $post_source, $reprint_id = null, $file_img = null, $file_video = null, $file_data = null)
    {
        // 正则表达式匹配网址格式
        $pattern = '/^(https?|ftp):\/\/[^\s\/$.?#].[^\s]*$/i';
        // 使用 preg_match 函数进行匹配
        $scraping_text_link = '';
        if (preg_match($pattern, $content) === 1) {
            $scraping_text_link = $content;
            $service = new FetchArticleService();
            $fetchArticle = $service->fetchArticle($scraping_text_link);
            if ($fetchArticle['code'] != 200) {
                $this->error('失败原因:' . $fetchArticle['msg']);
            }
            $title = $fetchArticle['title'];
            $content = $fetchArticle['html_content'];
            $obj = new CommonService();
            $content = $obj->contentFiles($content);//获取文章内容中的文件链接地址,将不是本项目的文件存到本项目中,并更新对应的文件地址
        }

        $is_audit = \app\common\model\Config::where('name', 'is_auto_approve_news')->value('value') ?? 0;//
        $success = News::create([
            'scraping_text_link' => $scraping_text_link,
            'parent_id' => $category_id,
            'user_id' => $user['id'],
            'create_time' => time(),
            'post_title' => $title ?? null,
            'post_content' => $content,
            'post_keywords' => $post_keywords ?? null,//关键词
            'post_excerpt' => $post_excerpt ?? null,//摘要
            'post_source' => $post_source ?? null,//转载文章的来源
            'status' => $is_audit,//是否审核|0:否,1:是
            'reprint_id' => $reprint_id,
            'file_img' => $file_img,
            'file_video' => $file_video,
            'file_data' => $file_data,
        ]);
        if (!$success) {
            $this->error("发生错误:" . '添加失败');
        }
        //自动加积分
        if ($is_audit == 1 && $success->id) {
            //增加积分
            $score = new Score();//调整积分
            $score->editScore($user['id'], $success->id, 'input_post_score', 1, 'news');
        }
        $this->success($is_audit ? "内容已发布" : "提交成功！请等待审核!");
    }

    //内容详情
    public function show()
    {
        $request = $this->request->param();//参数
        $category_type = $request['category_type'];//类型|plaza:广场帖子,plate:论坛帖子,news:新闻
        $id = $request['id'];//文章id

        $user = $this->auth->getUser();//用户信息
        switch ($category_type) {
            case 'plaza':
                $this->getPlazaShow($user, $id);
                break;
            case 'plate':
                $this->getPlateShow($user, $id);
                break;
            case 'news':
                $this->getNewsShow($user, $id);
                break;
            case 'video':
                $this->getVideoShow($user, $id);
                break;
            case 'live':
                $this->getLiveShow($user, $id);
                break;
            case 'investment':
                $this->getInvestmentShow($user, $id);
                break;
            default:
                $this->error('类型不正确');
        }
    }

    /**
     * 获取广场帖子详情
     * @param $user 用户信息
     * @param $id 文章/内容主健id
     */
    private function getPlazaShow($user, $id)
    {
        $model = PlazaPostModel::with('user')->find($id);
        if (!$model) {
            $this->error('内容不存在');
        }

        if ($model['user_id'] != $user['id']) {
            //检测用户是否有权访问内容
            $hasMembershipExpiryRecord = $this->hasMembershipExpiryRecordArticles($user['id'], 'plaza_post', $model['id'], $model['plaza_category_id']);
            if ($hasMembershipExpiryRecord['code'] != 200) {
                $this->error($hasMembershipExpiryRecord['msg']);
            }
        }

        //获取用户是否点赞,是否收藏
        $like = UserLike::where('user_id', $user['id'])
            ->where('table_name', 'plaza_post')
            ->where('object_id', $id)
            ->find();
        $favorite = UserFavorite::where('user_id', $user['id'])
            ->where('table_name', 'plaza_post')
            ->where('object_id', $id)
            ->find();

        $model = $model->toArray();

        if (!$model['user']) {
            $model['user'] = User::getSoftDeleteUser($model['user_id']);
        }
        $model['user']['avatar'] = obsurl($model['user']['avatar']);

        $file_img = [];
        foreach (explode(',', $model['file_img']) as $path) {
            $file_img[] = obsurl($path);
        }
        $file_video = [];
        foreach (explode(',', $model['file_video']) as $path) {
            $file_video[] = obsurl($path);
        }
        $file_audio = [];
        foreach (explode(',', $model['file_audio']) as $path) {
            $file_audio[] = obsurl($path);
        }
        $file_data = [];
        if ($model['file_data']) {
            $attachment = Attachment::whereIn('url', $model['file_data'])->select();
            foreach ($attachment as $av) {
                $file_data[] = [
                    'filename' => $av['filename'],
                    'url' => obsurl($av['url'])
                ];
            }
        }

        $reprint_data = [];//转发的原文信息
        if ($model['reprint_id']) {
            $reprint_article = Platepost::find($model['reprint_id']);//被转发文原文

            $r_file_img = [];
            foreach (explode(',', $reprint_article['file_img']) as $path) {
                $r_file_img[] = obsurl($path);
            }
            $r_file_video = [];
            foreach (explode(',', $reprint_article['file_video']) as $path) {
                $r_file_video[] = obsurl($path);
            }
            $r_file_audio = [];
            foreach (explode(',', $reprint_article['file_audio']) as $path) {
                $r_file_audio[] = obsurl($path);
            }
            $r_file_data = [];
            if ($reprint_article['file_data']) {
                $r_attachment = Attachment::whereIn('url', $reprint_article['file_data'])->select();
                foreach ($r_attachment as $r_av) {
                    $r_file_data[] = [
                        'filename' => '复盘-' . $r_av['filename'],
                        'url' => obsurl($r_av['url'])
                    ];
                }
            }

            $reprint_data = [
                'category_type' => 'plate',//查看详情用|类型|plaza:广场帖子,plate:论坛帖子,news:新闻,video:视频,live:直播,investment:投研日志
                'comment_type' => 'bbs',//发布评论用|news:新闻,bbs:论坛,video:视频,plaza:广场,live:直播
                'id' => $reprint_article['id'],
                'post_title' => $reprint_article['postname'],//标题
                'post_content' => $reprint_article['postcontent'],//内容
                'post_content_text' => filteredText($reprint_article['postcontent']),//内容
                'file_img' => array_filter($r_file_img),//图片数组
                'file_img_num' => count(array_filter($r_file_img)),//图片数组
                'file_video' => array_filter($r_file_video),//视频数组
                'file_audio' => array_filter($r_file_audio),//音频数组
                'file_data' => array_filter($r_file_data),//文件数组
            ];
        }

        $content_info = [
            'id' => $model['id'],
            'title' => $model['postname'],//帖子标题
            'content' => $model['postcontent'],//帖子内容(富文本)
            'user_id' => $model['user_id'],//用户id
            'category_id' => $model['plaza_category_id'],//栏目ID
            'comment_count' => $model['comment_count'],//评论数量
            'post_like' => $model['post_like'],//点赞数量
            'post_favorites' => $model['post_favorites'],//收藏数
            'membership_price' => $model['membership_price'],//会员价格(元)|默认0:免费
            'create_time' => $model['createtime'],//创建时间
            'is_like' => $like ? 1 : 0,//是否点赞|0:否
            'is_favorite' => $favorite ? 1 : 0,//是否收藏|0:否
            'user' => $model['user'],
            'is_author' => 0,//是否为作者|0:否,1:是

            'file_img' => array_filter($file_img),//图片数组
            'file_img_num' => count(array_filter($file_img)),//图片数组
            'file_video' => array_filter($file_video),//视频数组
            'file_audio' => array_filter($file_audio),//音数数组
            'file_data' => array_filter($file_data),//文件数组
            'reprint_id' => $model['reprint_id'],//转发的原文id|值>=1=转发,值不为真=原创
            'reprint_data' => $reprint_data,//转发的原文数据
            'is_reprint' => $this->is_reprint($model['postcontent']),//判断文章内容是否是转载 1:是,0:否
        ];
        if ($user['id'] == $model['user_id']) {
            $channels_info['is_author'] = 1;//是否为作者|0:否,1:是
        }

        $channels = Channels::where('parent_id', 1)->where('correlation_id', $model['plaza_category_id'])->find();
        $channels_info = [];
        if ($channels) {
            $channels_info = [
                'id' => $channels['id'],//
                'p_id' => $channels['parent_id'],//父id|0:顶级
                'name' => $channels['name'],//板块名称
                'is_comment' => $channels['iscomment'],//是否评论|0:否,1:是
                'describe' => $channels['description'],//分类描述
                'status' => $channels['status'],//状态,1全部可见 2会员全部可见 3部分会员可见.0隐藏
            ];
        }

        //评论
        $comment = \app\admin\model\cms\Comment::where('type', 'plaza')
            ->where('aid', $model['id'])
            ->where('pid', 0)
            ->where('fa_comment.status', 'normal')
            ->with(['user'])
            ->order('id', 'desc')
            ->limit(10)
            ->withCount('children') // 获取子级数据数量
            ->select();
        $comment_list = [];
        if ($comment) {
            $comment_list = $comment;
            foreach ($comment_list as &$v) {
                $v['createtime'] = $v['createtime'] ? date('Y-m-d H:i:s', $v['createtime']) : '';
                if (!$v['user']) {
                    $v['user'] = User::getSoftDeleteUser($v['user_id']);
                }
                $v['user']['avatar'] = obsurl($v['user']['avatar']);
                $v['is_author'] = 0;//是否为评论作者|1:是,0:否
                if ($v['user']['id'] == $user['id']) {
                    $v['is_author'] = 1;//是否为评论作者|1:是,0:否
                }
            }
        }

        //获取评论列表
        $res = [
            'plazaInfo' => $content_info,//内容
            'channelsInfo' => $channels_info,//栏目信息
            'commentList' => $comment_list,//评论
        ];

        PlazaPostModel::where('id', $id)->inc('view')->update();

        $this->success('', $res);
    }

    //获取论坛帖子详情
    private function getPlateShow($user, $id)
    {
        $model = Platepost::with('user')->find($id);
        if (!$model) {
            $this->error('内容不存在');
        }

        if ($model['user_id'] != $user['id']) {
            //检测用户是否有权访问内容
            $hasMembershipExpiryRecord = $this->hasMembershipExpiryRecordArticles($user['id'], 'plate_post', $model['id'], $model['plateid']);
            if ($hasMembershipExpiryRecord['code'] != 200) {
                $this->error($hasMembershipExpiryRecord['msg']);
            }
        }

        //获取用户是否点赞,是否收藏
        $like = UserLike::where('user_id', $user['id'])
            ->where('table_name', 'plate_post')
            ->where('object_id', $id)
            ->find();
        $favorite = UserFavorite::where('user_id', $user['id'])
            ->where('table_name', 'plate_post')
            ->where('object_id', $id)
            ->find();

        $model = $model->toArray();

        if (!$model['user']) {
            $model['user'] = User::getSoftDeleteUser($model['user_id']);//查询软删除的用户信息
        }
        $model['user']['avatar'] = obsurl($model['user']['avatar']);

        $file_img = [];
        foreach (explode(',', $model['file_img']) as $path) {
            $file_img[] = obsurl($path);
        }
        $file_video = [];
        foreach (explode(',', $model['file_video']) as $path) {
            $file_video[] = obsurl($path);
        }
        $file_audio = [];
        foreach (explode(',', $model['file_audio']) as $path) {
            $file_audio[] = obsurl($path);
        }

        $file_data = [];
        if ($model['file_data']) {
            $attachment = Attachment::whereIn('url', $model['file_data'])->select();
            foreach ($attachment as $av) {
                $file_data[] = [
                    'filename' => $av['filename'],
                    'url' => obsurl($av['url'])
                ];
            }
        }

        $reprint_data = [];//转发的原文信息
        if ($model['reprint_id']) {
            $reprint_article = Platepost::find($model['reprint_id']);//被转发文原文

            $r_file_img = [];
            foreach (explode(',', $reprint_article['file_img']) as $path) {
                $r_file_img[] = obsurl($path);
            }
            $r_file_video = [];
            foreach (explode(',', $reprint_article['file_video']) as $path) {
                $r_file_video[] = obsurl($path);
            }
            $r_file_audio = [];
            foreach (explode(',', $reprint_article['file_audio']) as $path) {
                $r_file_audio[] = obsurl($path);
            }
            $r_file_data = [];
            if ($reprint_article['file_data']) {
                $r_attachment = Attachment::whereIn('url', $reprint_article['file_data'])->select();
                foreach ($r_attachment as $r_av) {
                    $r_file_data[] = [
                        'filename' => '复盘-' . $r_av['filename'],
                        'url' => obsurl($r_av['url'])
                    ];
                }
            }

            $reprint_data = [
                'category_type' => 'plate',//查看详情用|类型|plaza:广场帖子,plate:论坛帖子,news:新闻,video:视频,live:直播,investment:投研日志
                'comment_type' => 'bbs',//发布评论用|news:新闻,bbs:论坛,video:视频,plaza:广场,live:直播
                'id' => $reprint_article['id'],
                'post_title' => $reprint_article['postname'],//标题
                'post_content' => $reprint_article['postcontent'],//内容
                'post_content_text' => filteredText($reprint_article['postcontent']),//内容
                'file_img' => array_filter($r_file_img),//图片数组
                'file_img_num' => count(array_filter($r_file_img)),//图片数组
                'file_video' => array_filter($r_file_video),//视频数组
                'file_audio' => array_filter($r_file_audio),//音频数组
                'file_data' => array_filter($r_file_data),//文件数组
            ];
        }


        $content_info = [
            'id' => $model['id'],
            'title' => $model['postname'],//帖子标题
            'content' => $model['postcontent'],//帖子内容(富文本)
            'user_id' => $model['user_id'],//用户id
            'category_id' => $model['plateid'],//栏目ID
            'comment_count' => $model['comment_count'],//评论数量
            'post_like' => $model['post_like'],//点赞数量
            'post_favorites' => $model['post_favorites'],//收藏数
            'membership_price' => $model['membership_price'],//会员价格(元)|默认0:免费
            'create_time' => date('Y-m-d H:i',strtotime($model['creat_time'])),//创建时间
            'is_like' => $like ? 1 : 0,//是否点赞|0:否
            'is_favorite' => $favorite ? 1 : 0,//是否收藏|0:否
            'user' => $model['user'],
            'is_author' => 0,//是否为作者|0:否,1:是

            'file_img' => array_filter($file_img),//图片数组
            'file_img_num' => count(array_filter($file_img)),//图片数组
            'file_video' => array_filter($file_video),//视频数组
            'file_audio' => array_filter($file_audio),//音数数组
            'file_data' => array_filter($file_data),//文件数组
            'reprint_id' => $model['reprint_id'],//转发的原文id|值>=1=转发,值不为真=原创
            'reprint_data' => $reprint_data,//转发的原文数据
            'is_reprint' => $this->is_reprint($model['postcontent']),//判断文章内容是否是转载 1:是,0:否
        ];
        if ($user['id'] == $model['user_id']) {
            $channels_info['is_author'] = 1;//是否为作者|0:否,1:是
        }

        //获取菜单信息
        $channels = Channels::where('parent_id', 3)->where('correlation_id', $model['plateid'])->find();
        $channels_info = [];
        if ($channels) {
            $channels_info = [
                'id' => $channels['id'],//
                'p_id' => $channels['parent_id'],//父id|0:顶级
                'name' => $channels['name'],//板块名称
                'is_comment' => $channels['iscomment'],//是否评论|0:否,1:是
                'describe' => $channels['description'],//分类描述
                'status' => $channels['status'],//状态,1全部可见 2会员全部可见 3部分会员可见.0隐藏
            ];
        }

        //评论
        $comment = \app\admin\model\cms\Comment::where('type', 'bbs')
            ->where('aid', $model['id'])
            ->where('pid', 0)
            ->where('fa_comment.status', 'normal')
            ->with(['user', 'children' => function ($query) {
                $query->with(['children']); // 添加其他关联关系
            }])
            ->order('id', 'desc')
            ->limit(10)
            ->select();
        $comment_list = [];
        if ($comment) {
            $comment_list = $comment;
            foreach ($comment_list as $k => &$v) {
                $v = $v->toArray();
                if (!$v['content_files']) {
                    $v['content_files'] = '';
                }
                $v['createtime'] = $v['createtime'] ? date('Y-m-d H:i:s', $v['createtime']) : '';
                if (!$v['user']) {
                    $v['user'] = User::getSoftDeleteUser($v['user_id']);
                }
                $v['user']['avatar'] = obsurl($v['user']['avatar']);
                $v['is_children'] = $v['children'] ? 1 : 0;//是否存在子集|0:否,1:是
                $v['children_count'] = count($v['children']);

                //如果自集评论大于10条则
                if ($v['children_count'] > 10) {
                    $v['children'] = array_slice($v['children'], 0, 10);
                }

                $v['is_author'] = 0;//是否为评论作者|1:是,0:否
                if ($v['user']['id'] == $user['id']) {
                    $v['is_author'] = 1;//是否为评论作者|1:是,0:否
                }
                $v['aaa'] = 0;//前端使用,用来判断是否修改文章,这个字段在后端无意义,固定就返回0
                foreach ($v['children'] as &$cv) {

                    $cv['aaa'] = 0;//前端使用,用来判断是否修改文章,这个字段在后端无意义,固定就返回0
                    $cv['comment_image'] = '';//前端使用,这个字段在后端无意义,固定就返回0空string
                    $cv['user'] = User::getSoftDeleteUser($cv['user_id']);
                    $cv['user']['avatar'] = $cv['user']['avatar_obs'];
                    $cv['createtime'] = $cv['createtime'] ? date('Y-m-d H:i:s', $cv['createtime']) : '';

                    $cv['is_author'] = 0;//是否为评论作者|1:是,0:否
                    if ($cv['user']['id'] == $user['id']) {
                        $cv['is_author'] = 1;//是否为评论作者|1:是,0:否
                    }

                    foreach ($cv['children'] as &$c_vv) {
                        $c_vv['createtime'] = $c_vv['createtime'] ? date('Y-m-d H:i:s', $c_vv['createtime']) : '';
                        $c_vv['user'] = User::getSoftDeleteUser($c_vv['user_id']);
                        $c_vv['user']['avatar'] = $c_vv['user']['avatar_obs'];

                        $c_vv['is_author'] = 0;//是否为评论作者|1:是,0:否
                        if ($c_vv['user']['id'] == $user['id']) {
                            $c_vv['is_author'] = 1;//是否为评论作者|1:是,0:否
                        }

                    }
                }
            }
        }

        //获取评论列表
        $res = [
            'plateInfo' => $content_info,//内容
            'channelsInfo' => $channels_info,//栏目信息
            'commentList' => $comment_list,//评论
        ];

        Platepost::where('id', $id)->inc('view')->update();

        $this->success('', $res);
    }

    //获取新闻帖子详情
    private function getNewsShow($user, $id)
    {
        $model = News::with('user')->find($id);
        if (!$model) {
            $this->error('内容不存在');
        }

        if ($model['user_id'] != $user['id']) {
            //检测用户是否有权访问内容
            $hasMembershipExpiryRecord = $this->hasMembershipExpiryRecordArticles($user['id'], 'news', $model['id'], $model['parent_id']);
            if ($hasMembershipExpiryRecord['code'] != 200) {
                $this->error($hasMembershipExpiryRecord['msg']);
            }
        }


        //获取用户是否点赞,是否收藏
        $like = UserLike::where('user_id', $user['id'])
            ->where('table_name', 'news')
            ->where('object_id', $id)
            ->find();
        $favorite = UserFavorite::where('user_id', $user['id'])
            ->where('table_name', 'news')
            ->where('object_id', $id)
            ->find();

        $model = $model->toArray();

        if (!$model['user']) {
            $model['user'] = User::getSoftDeleteUser($model['user_id']);
        }
        $model['user']['avatar'] = obsurl($model['user']['avatar']);

        $file_img = [];
        foreach (explode(',', $model['file_img']) as $path) {
            $file_img[] = obsurl($path);
        }
        $file_video = [];
        foreach (explode(',', $model['file_video']) as $path) {
            $file_video[] = obsurl($path);
        }
        $file_audio = [];
        foreach (explode(',', $model['file_audio']) as $path) {
            $file_audio[] = obsurl($path);
        }
        $file_data = [];
        if ($model['file_data']) {
            $attachment = Attachment::whereIn('url', $model['file_data'])->select();
            foreach ($attachment as $av) {
                $file_data[] = [
                    'filename' => $av['filename'],
                    'url' => obsurl($av['url'])
                ];
            }
        }

        $content_info = [
            'id' => $model['id'],
            'title' => $model['post_title'],//帖子标题
            'content' => $model['post_content'],//帖子内容(富文本)
            'user_id' => $model['user_id'],//用户id
            'category_id' => $model['parent_id'],//栏目ID
            'comment_count' => $model['comment_count'],//评论数量
            'post_like' => $model['post_like'],//点赞数量
            'post_favorites' => $model['post_favorites'],//收藏数
            'membership_price' => $model['membership_price'],//会员价格(元)|默认0:免费
            'create_time' => $model['create_time'] ? date('Y-m-d H:i:s', $model['create_time']) : '',//创建时间
            'is_like' => $like ? 1 : 0,//是否点赞|0:否
            'is_favorite' => $favorite ? 1 : 0,//是否收藏|0:否
            'user' => $model['user'],
            'is_author' => 0,//是否为作者|0:否,1:是

            'file_img' => array_filter($file_img),//图片数组
            'file_img_num' => count(array_filter($file_img)),//图片数组
            'file_video' => array_filter($file_video),//视频数组
            'file_audio' => array_filter($file_audio),//音数数组
            'file_data' => array_filter($file_data),//文件数组
            'is_reprint' => $this->is_reprint($model['post_content']),//判断文章内容是否是转载 1:是,0:否
        ];
        if ($user['id'] == $model['user_id']) {
            $channels_info['is_author'] = 1;//是否为作者|0:否,1:是
        }

        $channels = Channels::where('parent_id', 5)->where('correlation_id', $model['parent_id'])->find();
        $channels_info = [];
        if ($channels) {
            $channels_info = [
                'id' => $channels['id'],//
                'p_id' => $channels['parent_id'],//父id|0:顶级
                'name' => $channels['name'],//板块名称
                'is_comment' => $channels['iscomment'],//是否评论|0:否,1:是
                'describe' => $channels['description'],//分类描述
                'status' => $channels['status'],//状态,1全部可见 2会员全部可见 3部分会员可见.0隐藏
            ];
        }

        //评论
        $comment = \app\admin\model\cms\Comment::where('type', 'news')
            ->where('aid', $model['id'])
            ->where('pid', 0)
            ->where('fa_comment.status', 'normal')
            ->with(['user', 'children'])
            ->order('id', 'desc')
            ->limit(10)
            ->select();
        $comment_list = [];
        if ($comment) {
            $comment_list = $comment;
            foreach ($comment_list as &$v) {
                $v = $v->toArray();
                $v['createtime'] = $v['createtime'] ? date('Y-m-d H:i:s', $v['createtime']) : '';
                if (!$v['user']) {
                    $v['user'] = User::getSoftDeleteUser($v['user_id']);
                }
                $v['user']['avatar'] = obsurl($v['user']['avatar']);
                $v['is_children'] = $v['children'] ? 1 : 0;//是否存在子集|0:否,1:是
                $v['children_count'] = count($v['children']);//获取子级数据数量

                $v['is_author'] = 0;//是否为评论作者|1:是,0:否
                if ($v['user']['id'] == $user['id']) {
                    $v['is_author'] = 1;//是否为评论作者|1:是,0:否
                }
            }
        }

        //获取评论列表
        $res = [
            'newsInfo' => $content_info,//内容
            'channelsInfo' => $channels_info,//栏目信息
            'commentList' => $comment_list,//评论
        ];

        News::where('id', $id)->inc('post_hits')->update();//浏览自增

        $this->success('', $res);
    }

    //获取视频详情
    private function getVideoShow($user, $id)
    {
        $model = Video::with('user')->find($id);
        if (!$model) {
            $this->error('内容不存在');
        }

        //检测用户是否有权访问内容
        $hasMembershipExpiryRecord = $this->hasMembershipExpiryRecordArticles($user['id'], 'video', $model['id'], $model['video_category_id']);
        if ($hasMembershipExpiryRecord['code'] != 200) {
            $this->error($hasMembershipExpiryRecord['msg']);
        }

        //获取用户是否点赞,是否收藏
        $like = UserLike::where('user_id', $user['id'])
            ->where('table_name', 'video')
            ->where('object_id', $id)
            ->find();
        $favorite = UserFavorite::where('user_id', $user['id'])
            ->where('table_name', 'video')
            ->where('object_id', $id)
            ->find();

        $model = $model->toArray();
        if (!$model['user']) {
            $model['user'] = User::getSoftDeleteUser($model['user_id']);
        }
        $model['user']['avatar'] = obsurl($model['user']['avatar']);

        if ($model['video_path']) {
            if (substr($model['video_path'], 0, 1) === '/') {
                $model['video_path'] = obsurl($model['video_path']);
            }
        }
        if ($model['image']) {
            if (substr($model['image'], 0, 1) === '/') {
                $model['image'] = obsurl($model['image']);
            }
        }
        $content_info = [
            'id' => $model['id'],
            'title' => $model['title'],//标题
            'description' => $model['description'],//描述
            'image' => $model['image'],//封面图片
            'video_path' => $model['video_path'],//视频链接
            'type' => $model['type'],//类型|1:视频,2:音频
            'user_id' => $model['user_id'],//用户id
            'is_comment' => $model['iscomment'],//是否可以评论|0:否,1:是
            'category_id' => $model['video_category_id'],//栏目ID
            'comment_count' => $model['count_comment'],//评论数量
            'post_like' => $model['count_like'],//点赞数量
            'post_favorites' => $model['count_favorite'],//收藏数
            'membership_price' => $model['membership_price'],//会员价格(元)|默认0:免费
            'create_time' => (!$model['create_time'] ? '':date('Y-m-d H:i',strtotime($model['create_time']))),//创建时间
            'is_like' => $like ? 1 : 0,//是否点赞|0:否
            'is_favorite' => $favorite ? 1 : 0,//是否收藏|0:否
            'average_score' => $model['average_score'],//平均分
            'user' => $model['user']
        ];

        $channels = Channels::where('parent_id', 5)->where('correlation_id', $model['video_category_id'])->find();
        $channels_info = [];
        if ($channels) {
            $channels_info = [
                'id' => $channels['id'],//
                'p_id' => $channels['parent_id'],//父id|0:顶级
                'name' => $channels['name'],//板块名称
                'is_comment' => $channels['iscomment'],//是否评论|0:否,1:是
                'describe' => $channels['description'],//分类描述
                'status' => $channels['status'],//状态,1全部可见 2会员全部可见 3部分会员可见.0隐藏
            ];
        }

        //评论
        $comment = \app\admin\model\cms\Comment::where('type', 'video')
            ->where('aid', $model['id'])
            ->where('pid', 0)
            ->where('fa_comment.status', 'normal')
            ->with(['user', 'children'])
            ->order('id', 'desc')
            ->limit(10)
            ->select();
        $comment_list = [];
        if ($comment) {
            $comment_list = $comment;
            foreach ($comment_list as &$v) {
                $v['createtime'] = $v['createtime'] ? date('Y-m-d H:i:s', $v['createtime']) : '';
                if (!$v['user']) {
                    $v['user'] = User::getSoftDeleteUser($v['user_id']);
                }
                $v['user']['avatar'] = obsurl($v['user']['avatar']);
                $v['is_children'] = $v['children'] ? 1 : 0;//是否存在子集|0:否,1:是
                $v['children_count'] = count($v['children']);

                $v['is_author'] = 0;//是否为评论作者|1:是,0:否
                if ($v['user']['id'] == $user['id']) {
                    $v['is_author'] = 1;//是否为评论作者|1:是,0:否
                }
            }
        }

        //获取评论列表
        $res = [
            'videoInfo' => $content_info,//内容
            'channelsInfo' => $channels_info,//栏目信息
            'commentList' => $comment_list,//评论
        ];

        Video::where('id', $id)->inc('count_view')->update();//浏览自增

        $this->success('', $res);
    }

    //获取直播详情
    private function getLiveShow($user, $id)
    {
        $model = Live::find($id);
        if (!$model) {
            $this->error('内容不存在');
        }
        $model = $model->toArray();

        //检测用户是否有权访问内容
        $hasMembershipExpiryRecord = $this->hasMembershipExpiryRecordArticles($user['id'], 'live', $model['id'], $model['live_category_id']);
        if ($hasMembershipExpiryRecord['code'] != 200) {
            $this->error($hasMembershipExpiryRecord['msg']);
        }

        if ($model['image']) {
            if (substr($model['image'], 0, 1) === '/') {
                $model['image'] = obsurl($model['image']);
            }
        }
        $content_info = [
            'id' => $model['id'],
            'category_id' => $model['live_category_id'],//栏目ID
            'title' => $model['title'],//标题
            'description' => $model['description'],//描述
            'image' => $model['image'],//封面图片
            'live_url' => $model['live_url'],//视频链接
            'live_time' => $model['live_time'],//直播时间
            'is_top' => $model['is_top'],//是否置顶|0:否,1:是
            'membership_price' => $model['membership_price'],//会员价格(元)|默认0:免费
            'average_score' => $model['average_score'],//平均分
            'create_time' => $model['create_time'],//创建时间
            'rating' => RatingModel::where('table_name', 'live')
                    ->where('object_id', $id)
                    ->where('user_id', $user['id'])
                    ->value('score') ?? 0,//评分
        ];

        //栏目表信息
        $channels = Channels::where('parent_id', 5)->where('correlation_id', $model['live_category_id'])->find();
        $channels_info = [];
        if ($channels) {
            $channels_info = [
                'id' => $channels['id'],//
                'p_id' => $channels['parent_id'],//父id|0:顶级
                'name' => $channels['name'],//板块名称
                'is_comment' => $channels['iscomment'],//是否评论|0:否,1:是
                'describe' => $channels['description'],//分类描述
                'status' => $channels['status'],//状态,1全部可见 2会员全部可见 3部分会员可见.0隐藏
            ];
        }

        //评论
        $comment = \app\admin\model\cms\Comment::where('type', 'live')
            ->where('aid', $model['id'])
            ->where('pid', 0)
            ->where('fa_comment.status', 'normal')
            ->with(['user', 'children'])
            ->order('id', 'desc')
            ->limit(10)
            ->select();
        $comment_list = [];
        if ($comment) {
            $comment_list = $comment;
            foreach ($comment_list as &$v) {
                $v['createtime'] = $v['createtime'] ? date('Y-m-d H:i:s', $v['createtime']) : '';
                if (!$v['user']) {
                    $v['user'] = User::getSoftDeleteUser($v['user_id']);
                }
                $v['user']['avatar'] = obsurl($v['user']['avatar']);
                $v['is_children'] = $v['children'] ? 1 : 0;//是否存在子集|0:否,1:是
                $v['children_count'] = count($v['children']);

                $v['is_author'] = 0;//是否为评论作者|1:是,0:否
                if ($v['user']['id'] == $user['id']) {
                    $v['is_author'] = 1;//是否为评论作者|1:是,0:否
                }
            }
        }

        //获取评论列表
        $res = [
            'liveInfo' => $content_info,//内容
            'channelsInfo' => $channels_info,//栏目信息
            'commentList' => $comment_list,//评论
        ];
        $this->success('', $res);
    }

    //获取投研日志详情
    private function getInvestmentShow($user, $id)
    {
        $model = Investment::find($id);
        if (!$model) {
            $this->error('内容不存在');
        }

        //检测用户是否有权访问内容
        $hasMembershipExpiryRecord = $this->hasMembershipExpiryRecordArticles($user['id'], 'investment', $model['id'], $model['investment_category_id']);
        if ($hasMembershipExpiryRecord['code'] != 200) {
            $this->error($hasMembershipExpiryRecord['msg']);
        }

        $model = $model->toArray();

        //判断是否分类是只显示文件的
        $is_show_file = InvestmentCategoryModel::where('id', $model['investment_category_id'])->value('type');
        $file_list = [];
        if ($is_show_file == 1) {
            $file_list = json_decode($model['file_list'], true) ?? [];
        }


        $content_info = [
            'id' => $model['id'],
            'content_time' => $model['content_time'] ? date('Y-m-d H:i:s', $model['content_time']) : '',//时间
            'macro_content' => $model['macro_content'],//宏观面
            'policy_content' => $model['policy_content'],//政策面
            'fund_content' => $model['fund_content'],//资金面及预期
            'emotion_content' => $model['emotion_content'],//情绪面
            'expect_content' => $model['expect_content'],//市场预期/关注
            'news_content' => $model['news_content'],//市场新闻
            'create_time' => $model['createtime'] ? date('Y-m-d H:i:s', $model['createtime']) : '',//创建时间
            'is_show_file' => $is_show_file,//0=不展示文件,1=展示文件
            'file_list' => $file_list,
        ];

        //获取评论列表
        $res = [
            'investmentInfo' => $content_info,//内容
            'channelsInfo' => [],//栏目信息
            'commentList' => [],//评论
        ];
        $this->success('', $res);
    }

    //测试接口
    public function test()
    {
        $tengxunmeet = new TengxunSignatureUtil();//腾讯会议服务类
        $res = $tengxunmeet->getLiveReplays('10643291259139467936', '244596969');
        dd($res);

//        $r = $this->request->param();
//        $filePath = 'login/test.txt';
//        $date = date('Y-m-d H:i:s');
//        $data = "回调成功了-{$date}"; // 要写入的数据
//        file_put_contents($filePath, $data);
//        $a = $huaWeiSMS->send('15335526445','123123');
//dd($a);
//        $user = $this->auth->getUser();//用户信息
//        $a = new User();
//        $aa = $a->isChannelsPermission($user, 18);
//        dd($aa, $user->toArray());
//        dd($res1, $res2);
    }

    //获取分类树形列表
    public function getCategoryList()
    {
        $user = $this->auth->getUser();//用户信息
        $pid = $this->request->param('channels_id', 0);//Channels表pid|默认为0:获取顶级分类
        $column = Channels::where('status', 'in', [1, 2, 3]);
        if ($pid) {
            $column = $column->where('parent_id', $pid);
        }
        $column = $column->order('list_order', 'desc')
            ->select();
        $data = Channels::buildTree($column, $pid);//获取分类树形结构

        $column_rule_model = new User();
        $column_rule = $column_rule_model->getUserChannels($user);//通过用户模型获取用户的频道
        foreach ($data as $key => &$value) {
            //判断顶级分类id是否在权限组中
            $value['authorize'] = false;
            if ($value['status'] == 1 || in_array($value['id'], $column_rule)) {
                $value['authorize'] = true;//是否有权访问|true:是,false:否
            }
            //判断2级分类id是否在权限组中
            foreach ($value['children'] as &$vv) {
                //如果顶级分类允许访问,那么2级分类就全部允许访问
                $vv['authorize'] = false;
                if ($value['authorize']) {
                    $vv['authorize'] = true;
                } else {
                    if ($vv['status'] == 1 || in_array($vv['id'], $column_rule)) {
                        $vv['authorize'] = true;
                    }
                }
            }
        }

        $this->success('获取成功', $data);
    }

    //日历标记,那天获得了积分那天就变颜色（指定月份，有文章/视频的日期标记出来
    public function calendarTag()
    {
        $param = $this->request->param();
        if (!isset($param['year_month']) || strlen($param['year_month']) != 7) {
            $this->error("日期参数错误！！");
        }
//        $category_type = $param['category_type'];//类型|plaza:广场帖子,news:新闻,video:视频,plate:论坛
//        $channels_id = $param['category_id'];//分类id|fa_channels表id
//        $category_id = Channel::where('id', $channels_id)->value('correlation_id');//需要把转换下correlation_id
        $start_time = strtotime($param['year_month'] . '-01 00:00:00');
        $end_time = strtotime(sprintf('%s +1month', $param['year_month'] . '-01 00:00:00'));

        //获取那天获得了积分
        $user = $this->auth->getUser();//用户信息
        $list = \app\admin\model\Score::where('user_id', $user['id'])
            ->where('seq_flag', 1)
            ->whereBetween('change_time', [$start_time, $end_time])
            ->select();

        $temp = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'];
        $month = [];
        foreach ($temp as $v) {
            $month [sprintf('%s-%s', $param['year_month'], $v)] = 0;
        }
        foreach ($list as $v) {
            // 天数不要前面的0
            $day = date('Y-m-d', $v['change_time']);
            isset($month[$day]) && $month[$day] += 1;
        }
        $this->success('', ['days' => $month, 'month' => $param['year_month']]);
    }

    //扫二维码获取直播详情
    public function getQrCodeLiveShow()
    {
        $request = $this->request->param();
        $id = $request['id'];//直播id
        $data = Live::find($id);
        $meetInfo = [];
        if (!$data) {
            $this->error('信息不存在');
        }
        if ($data['is_start_live'] == 1 && $data['meet_info']) {
            $meetInfo = json_decode($data['meet_info'], true);
            $tengxunmeet = new TengxunSignatureUtil();
            $push_address = 'rtmp://' . $data['push_url'] . '/' . $data['live_app_name'] . '/';
            $push_password = str_replace($push_address, '', $data['push_live_url']);
            $tengxunmeet->setliveStreamAddress($meetInfo['meeting_info_list'][0]['meeting_id'], $push_address, $push_password);//设置第三方直播推流地址
        }
        if ($this->request->isPost()) {
            $password = $request['password'] ?? '';//直播密码
            if ($data['password']) {
                if ($data['password'] != $password) {
                    $this->error('请输入直播密码');
                }
            }

            $this->success('success', $data);
        } else {
            $view = View::instance(Config::get('template'), Config::get('view_replace_str'));
            return $view->fetch('getQrCodeLiveShow', ['data' => $data, 'meetinfo' => $meetInfo]);
        }
    }

    //获取当前直播状态
    public function getLiveState()
    {
        $request = $this->request->param();
        $id = $request['id'];
        $data = Live::where('id', $id)->where('is_start_live', 1)->find();
        if (!$data) {
            $this->error('直播未开始/信息有误');
        }
        $huaWeiLive = new HuaWeiLive();
        $res_data = $huaWeiLive->listLiveStreamsOnline($data['push_url'], 'live_app_name', null, null, $data['live_stream_name']);
        if ($res_data['code'] != 200) {
            $this->error($data['msg']);
        }

        $res = [
            'is_live' => 0//是否直播中|0:否,1:是
        ];
        if ($res_data['data']['streams'] ?? []) {
            $res['is_live'] = 1;
        }
        $this->success('', $res);
    }

    //获取直播预告
    public function getLiveForeshow()
    {
        $user = $this->auth->getUser();//用户信息
        $limit = $this->request->param('limit', 10);
        $time = time();
        //开播时间 > 当前的
        $model = Live::where('live_time', '>', $time)->order('is_top', 'desc')->order('live_time', 'asc')->limit(10)->select();
        $res = [];
        foreach ($model as $v) {
            $res[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'description' => $v['description'],
                'image' => obsurl($v['image']),
                'live_time' => $v['live_time'],
            ];
        }
        $this->success('', $res);
    }

    //获取快速发帖分类id(债市论坛发帖直达)
    public function getPlatePostChannelsId()
    {
        $model = \app\common\model\Config::where('name', 'plate_post_channels_id')->value('value') ?? 0;
        $res = [
            'plate' => [
                'category_id' => $model,
                'category_type' => 'plate',
            ]
        ];
        $this->success('', $res);
    }

    //下载论坛帖子附件
    public function downloadAccessory()
    {
        $user = $this->auth->getUser();//用户信息
        $id = $this->request->param('id', 0);
        if (!$id) {
            $this->error('缺少参数');
        }

        //获取附件链接
        $accessory = Platepost::where('id', $id)->value('accessory');
        if (!$accessory) {
            $this->error('当前附件不存在');
        }
        //获取用户剩余积分
        $user_score = User::where('id', $user['id'])->value('score');
        //从config中的到下载附件应该扣除的积分值
        $attachment_download_score = \app\common\model\Config::where('name', 'attachment_download_score')->value('value');
        //判断用户积分是否足够下载附件
        if ($user_score < $attachment_download_score) {
            $this->error('积分余额不足');
        }
        //扣除用户积分
        $scoreObj = new Score();
        $a = $scoreObj->editScore($user['id'], $id, 'attachment_download_score', 2, 'plate_post');
        //执行下载附件
        $res = [
            'accessory' => obsurl($accessory)
        ];
        $this->success('', $res);
    }

    //获取分类首页数据列表接口
    public function getHomeList()
    {
        $user = $this->auth->getUser();//用户信息
        $category_type = $this->request->param('category_type', 0);//类型|plate:论坛帖子,news:新闻,video:视频,live:直播,investment:投研日志
        $channels_id = $this->request->param('channels_id', 0);//栏目表id(1级id)
        $page = $this->request->param('page', 1);//页码
        $limit = $this->request->param('limit', 20);//每页数量
        $keyword = $this->request->param('keyword', '');//关键词
        $time = $this->request->param('time', null);//时间筛选(格式:2023-09-13)
        $start_time = $this->request->param('start_time', '');//时间筛选(格式:2023-09-13)
        $end_time = $this->request->param('end_time', '');//时间筛选(格式:2023-09-13)
        $type = $this->request->param('type', 4) ?: 4;//类型1:今日,2:七日内发布的内容,3:本月发布的,4:今年发布的
        $orderway = $this->request->param('orderway', 'desc');//排序方式:asc,desc

        if (!$category_type) {
            $this->error('分类类型不正确');
        }

        $new_user = new User();
        $rule_column_id = $new_user->isChannelsPermission($user)['rule_column_id'];//当前用户拥有权限的的全部栏目id
        //获取首页2级栏目
        $channels = Channels::where('parent_id', $channels_id)
            ->where('status', '>', 0)
            ->field('id,correlation_id')
            ->select();

        //检测自有分类id是否有查看权限
        $membershipExpiryRecord = new MembershipExpiryRecord();
        //当前全部自有分类的id(就是2级分类id)
        $category_id = [];
        foreach ($channels as $v) {
            $category_id[] = $v['correlation_id'];
        }

        switch ($category_type) {
            case 'plate'://论坛
                $category_table_name = 'plate';
                $category_model = new Plate();
                break;
            case 'news'://新闻
                $category_table_name = 'news_category';
                $category_model = new NewsCategoryModel();
                break;
            case 'video'://视频
                $category_table_name = 'video_category';
                $category_model = new VideoCategoryModel();
                break;
            case 'live'://直播
                $category_table_name = 'live_category';
                $category_model = new LiveCategoryModel();
                break;
            case 'investment'://投研日志
                $category_table_name = 'investment_category';
                $category_model = new InvestmentCategoryModel();
                break;
            case 'chart'://图表
                $category_table_name = 'chart_parent';
                $category_model = new Chart();
                break;
            default:
                $this->error('类型不正确');
        }

        $free_catedory_id_arr = [];//需要判断内容是否收费的自有分类id|默认空
        //判断顶级id不在在会员权限数组中
        if (!in_array($channels_id, $rule_column_id)) {
            //不在会员权限数组中,说明用户组可能只有部分2级分类的权限
            //查对应的自有分类表需要付费的分类id
            $pay_catedory_id = $category_model->whereIn('id', $category_id)->where('membership_price', '>', 0)->column('id');

            //查对应的自有分类表免费的分类id
            $free_catedory_id_arr = $category_model->whereIn('id', $category_id)->where('membership_price', 0)->column('id');

            //判断用户是否购买了付费的分类,没买则把这个分类id从$category_id数组中删掉
            foreach ($pay_catedory_id as $v) {
                //用户没买会员组也没买对应分类,经过判断得到用户可以看到的自有分类id
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'categories', $category_table_name, $v);
                if ($isMemberExpire['code'] != 200) {
                    $category_id = array_diff($category_id, [$v]);//数组差集
                }
            }
        }
        //拼装方法名称
        $function_name = 'get' . ucfirst($category_type) . 'Home';
        $time_arr = [
            'time' => $time,
            'start_time' => $start_time,
            'end_time' => $end_time,
        ];
        $res = $this->$function_name($user, $category_id, $free_catedory_id_arr, $page, $limit, $keyword, $time_arr, $type, $orderway, $membershipExpiryRecord);
        $this->success('', $res);
    }

    //获取论坛-首页内容列表
    private function getPlateHome($user, $category_id_arr, $free_catedory_id_arr, $page, $limit, $keyword, $time_arr, $type, $orderway, $membershipExpiryRecord = null)
    {

        $data = Platepost::whereIn('plateid', $category_id_arr);
        if ($keyword) {
            $data->where('postname', 'like', "%{$keyword}%");
        }

        $time = $time_arr['time'];
        if ($time) {
            $data->where('creat_time', '<=', $time);
        }
        $getStartAndEndTime = $this->getStartAndEndTime($type);//根据类型获取日期时间
        $data->where('creat_time', '>=', $getStartAndEndTime['start_time']);
        $data = $data->with(['user'])->order('top', 'desc')->order('is_jing', 'desc')->order('id', $orderway)->paginate($limit, false, ['page' => $page])->toArray();

        //附件列表
        $attachment_url_arr = [];
        foreach ($data['data'] as $v) {
            if ($v['file_data']) {
                $attachment_url_arr = array_merge($attachment_url_arr, explode(',', $v['file_data']));
            }
        }
        //获取附件列表
        $attachment_list = (new AttachmentService())->getList(['url' => array_unique(array_filter($attachment_url_arr))]);

        $arList = [];
        foreach ($data['data'] as $v) {
            $commentData = Comment::where('pid', 0)->where('aid', $v['id'])
                ->where('type', 'bbs')
                ->with(['user', 'children'])
                ->limit(5)->select();

            $commentList = [];
            foreach ($commentData as $ck => $cv) {
                $commentList[$ck] = $cv->toArray();
                $commentList[$ck]['createtime'] = date('Y-m-d H:i:s', $commentList[$ck]['createtime']);
                if (!$cv['user']) {
                    $cv['user'] = User::getSoftDeleteUser($cv['user_id']);
                }
                $commentList[$ck]['user'] = [
                    'id' => $cv['user']['id'] ?? '',
                    'username' => $cv['user']['nickname'] ?? '',
                    'avatar' => obsurl($cv['user']['avatar'] ?? ''),
                ];
                $commentList[$ck]['is_children'] = $cv['children'] ? 1 : 0;//是否存在子集|0:否,1:是
            }
            $is_mylike = UserLike::where('table_name', 'plate_post')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->value('id');
            $is_myfavorite = UserFavorite::where('table_name', 'plate_post')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->value('id');

            if (!$v['user']) {
                $v['user'] = User::getSoftDeleteUser($v['user_id']);
            }

            if ($v['user']['avatar'] ?? '') {
                $v['user']['avatar'] = obsurl($v['user']['avatar']);
            }
            if ($v['file']) {
                $v['file'] = obsurl($v['file']);
            }

            $file_img = [];
            foreach (explode(',', $v['file_img']) as $path) {
                $file_img[] = obsurl($path);
            }
            $file_video = [];
            foreach (explode(',', $v['file_video']) as $path) {
                $file_video[] = obsurl($path);
            }
            $file_audio = [];
            foreach (explode(',', $v['file_audio']) as $path) {
                $file_audio[] = obsurl($path);
            }
            $file_data = [];
            if ($v['file_data']) {
                $v['file_data'] = explode(',', $v['file_data']);
                foreach($v['file_data'] as $fv){
                    foreach ($attachment_list as $av) {
                        if ($av['url'] == $fv){
                            $file_data[] = [
                                'filename' => $av['filename'],
                                'url' => obsurl($av['url'])
                            ];
                        }
                    }
                }
            }

            $reprint_data = [];//转发的原文信息
            if ($v['reprint_id']) {
                $reprint_article = Platepost::find($v['reprint_id']);//被转发文原文

                $r_file_img = [];
                foreach (explode(',', $reprint_article['file_img']) as $path) {
                    $r_file_img[] = obsurl($path);
                }
                $r_file_video = [];
                foreach (explode(',', $reprint_article['file_video']) as $path) {
                    $r_file_video[] = obsurl($path);
                }
                $r_file_audio = [];
                foreach (explode(',', $reprint_article['file_audio']) as $path) {
                    $r_file_audio[] = obsurl($path);
                }
                $r_file_data = [];
                if ($reprint_article['file_data']) {
                    $r_attachment = Attachment::whereIn('url', $reprint_article['file_data'])->select();
                    foreach ($r_attachment as $r_av) {
                        $r_file_data[] = [
                            'filename' => '复盘-' . $r_av['filename'],
                            'url' => obsurl($r_av['url'])
                        ];
                    }
                }

                $reprint_data = [
                    'category_type' => 'plate',//查看详情用|类型|plaza:广场帖子,plate:论坛帖子,news:新闻,video:视频,live:直播,investment:投研日志
                    'comment_type' => 'bbs',//发布评论用|news:新闻,bbs:论坛,video:视频,plaza:广场,live:直播
                    'id' => $reprint_article['id'],
                    'post_title' => $reprint_article['postname'],//标题
                    'post_content' => $reprint_article['postcontent'],//内容
                    'post_content_text' => filteredText($reprint_article['postcontent']),//内容
                    'file_img' => array_filter($r_file_img),//图片数组
                    'file_img_num' => count(array_filter($r_file_img)),//图片数组
                    'file_video' => array_filter($r_file_video),//视频数组
                    'file_audio' => array_filter($r_file_audio),//音频数组
                    'file_data' => array_filter($r_file_data),//文件数组
                ];
            }

            $creat_time = !$v['creat_time'] ? '':(date('Y-m-d H:i',strtotime($v['creat_time'])));

            $content_arr = [
                'table_name' => 'plate_post',//点赞收藏用|内容以前所在表,不带前缀|news:新闻,plaza_post:广场,video:视频,plate_post:论坛
                'category_type' => 'plate',//查看详情用|类型|plaza:广场帖子,plate:论坛帖子,news:新闻,video:视频,live:直播,investment:投研日志
                'comment_type' => 'bbs',//发布评论用|news:新闻,bbs:论坛,video:视频,plaza:广场,live:直播
                'id' => $v['id'],
                'post_title' => $v['postname'],//标题
                'post_content' => $v['postcontent'],//内容
                'post_favorites' => $v['post_favorites'] ?? 0,//收藏数量
                'post_forward' => 0,//转发数量
                'membership_price' => $v['membership_price'],//会员价格|0:免费
                'user_id' => $v['user_id'],//用户id
                'avatar' => $v['user']['avatar'] ?? '',//用户头像
                'username' => $v['user']['nickname'] ?? '',//昵称
                'thumbnail' => $v['file'],//图片链接
                'is_accessory' => ($v['accessory'] ? 1 : 0),//是否存在附件|0:否,1:是
                'is_top' => $v['top'],//是否置顶|0:否
                'is_jing' => $v['is_jing'],//是否加精|0:否
                'is_mylike' => $is_mylike ? 1 : 0,//是否喜欢|0:否
                'is_myfavorite' => $is_myfavorite ? 1 : 0,//是否收藏|0:否
                'recommended' => $v['is_jing'],//是否推荐|0:否
                'commentList' => $commentList,//顶级评论列表
                'comment_count' => $v['comment_count'],//评论数量
                'create_time' => $creat_time,//创建时间
                'post_like' => $v['post_like'],//点赞数量
                'post_hits' => 0,//点击量
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
                'post_content_text' => filteredText($v['postcontent']),//内容

                'file_img' => array_filter($file_img),//图片数组
                'file_img_num' => count(array_filter($file_img)),//图片数组
                'file_video' => array_filter($file_video),//视频数组
                'file_audio' => array_filter($file_audio),//音数数组
                'file_data' => array_filter($file_data),//文件数组
                'reprint_id' => $v['reprint_id'],//现在的原文id
                'reprint_data' => $reprint_data,//转发的原文数据
                'is_author' => 0,//是否为作者|0:否,1:是
            ];
            //是不是作者
            if ($user['id'] == $v['user_id']) {
                $content_arr['is_author'] = 1;
            } else {
                //判断内容是否收费
                if (in_array($v['plateid'], $free_catedory_id_arr) && (float)$v['membership_price'] >= 0.01) {
                    //检测用户是否有权访问内容
                    $hasMembershipExpiryRecord = $this->hasMembershipExpiryRecordArticles($user['id'], 'plate_post', $v['id'], $v['plateid']);
                    if ($hasMembershipExpiryRecord['code'] != 200) {
                        //用户没买分类
                        $content_arr['is_member_expire'] = 1;//会员是否到期|0:否,1:是
                    }
                }
            }

            $arList[] = $content_arr;
        }

        $res = [
            'plateList' => $arList,
            'total' => $data['total']
        ];
        return $res;
    }

    //获取新闻-首页内容列表
    private function getNewsHome($user, $category_id_arr, $free_catedory_id_arr, $page, $limit, $keyword, $time_arr, $type, $orderway, $membershipExpiryRecord)
    {
        $data = News::whereIn('parent_id', $category_id_arr)->where('post_status', '1')->where('status', '1');
        if ($keyword) {
            $data->where('post_content', 'like', "%{$keyword}%");
        }
        $time = $time_arr['time'];
        if ($time) {
            $data->where('published_time', '<=', strtotime($time));
        }

        $getStartAndEndTime = $this->getStartAndEndTime($type);//根据类型获取日期时间
        $data->where('published_time', '>=', strtotime($getStartAndEndTime['start_time']));

        $data = $data->with(['user'])->order('id', $orderway)->paginate($limit, false, ['page' => $page])->toArray();

        $arList = [];
        foreach ($data['data'] as $v) {
            $commentData = Comment::where('pid', 0)->where('aid', $v['id'])
                ->where('type', 'news')
                ->with(['user', 'children'])
                ->limit(5)->select();

            $commentList = [];
            foreach ($commentData as $ck => $cv) {
                $commentList[$ck] = $cv->toArray();
                $commentList[$ck]['createtime'] = date('Y-m-d H:i:s', $commentList[$ck]['createtime']);
                if (!$cv['user']) {
                    $cv['user'] = User::getSoftDeleteUser($cv['user_id']);
                }
                $commentList[$ck]['user'] = [
                    'id' => $cv['user']['id'] ?? '',
                    'username' => $cv['user']['nickname'] ?? '',
                    'avatar' => $cv['user']['avatar'] ?? '',
                ];
                $commentList[$ck]['is_children'] = $cv['children'] ? 1 : 0;//是否存在子集|0:否,1:是
            }

            $is_mylike = UserLike::where('table_name', 'news')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->find();
            $is_myfavorite = UserFavorite::where('table_name', 'news')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->find();

            if (!$v['user']) {
                $v['user'] = User::getSoftDeleteUser($v['user_id']);
            }

            if ($v['user']['avatar'] ?? '') {
                if (substr($v['user']['avatar'], 0, 1) === '/') {
                    $v['user']['avatar'] = obsurl($v['user']['avatar']);
                }
            }
            if ($v['thumbnail']) {
                if (substr($v['thumbnail'], 0, 1) === '/') {
                    $v['thumbnail'] = obsurl($v['thumbnail']);
                }
            }


            //内容数组
            $content_arr = [
                'id' => $v['id'],
                'post_title' => $v['post_title'],//标题
                'post_content' => $v['post_content'],//内容
                'post_favorites' => $v['post_favorites'] ?? 0,//收藏数量
                'post_like' => $v['post_like'],//点赞数量
                'post_hits' => 0,//点击量
                'post_forward' => 0,//转发数量
                'membership_price' => $v['membership_price'],//会员价格|0:免费
                'user_id' => $v['user_id'],//用户id
                'thumbnail' => $v['thumbnail'],//缩略图
                'avatar' => $v['user']['avatar'] ?? '',//头像
                'username' => $v['user']['nickname'] ?? '',//昵称
                'is_top' => $v['is_top'],//是否置顶|0:否
                'is_mylike' => $is_mylike ? 1 : 0,//是否喜欢|0:否
                'is_myfavorite' => $is_myfavorite ? 1 : 0,//是否收藏|0:否
                'recommended' => $v['recommended'],//是否推荐|0:否
                'commentList' => $commentList,//顶级评论列表
                'comment_count' => $v['comment_count'] ?? 0,//评论数量
                'create_time' => date('Y-m-d H:i:s', $v['published_time']),//创建时间
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
            ];
            //判断内容是否收费
            if (in_array($v['parent_id'], $free_catedory_id_arr) && (float)$v['membership_price'] >= 0.01) {
                //内容收费
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'articles', 'news', $v['id']);
                //判断开没开内容会员
                if ($isMemberExpire['code'] != 200) {
                    //没开内容会员
//                        $content_arr['post_title'] = '';
                    $content_arr['post_content'] = '';
                    $content_arr['is_member_expire'] = 1;//会员是否到期|0:否,1:是
                }
            }
            $arList[] = $content_arr;
        }
        $res = [
            'newsList' => $arList,
            'total' => $data['total']
        ];
        return $res;
    }

    //获取视频-首页内容列表
    private function getVideoHome($user, $category_id_arr, $free_catedory_id_arr, $page, $limit, $keyword, $time_arr, $type, $orderway, $membershipExpiryRecord)
    {
        $data = Video::whereIn('video_category_id', $category_id_arr);
        if ($keyword) {
            $data->where('title', 'like', "%{$keyword}%");
        }
        $time = $time_arr['time'];
        if ($time) {
            $data->where('create_time', '<=', strtotime($time));
        }

        $getStartAndEndTime = $this->getStartAndEndTime($type);//根据类型获取日期时间
        $data->where('create_time', '>=', strtotime($getStartAndEndTime['start_time']));

        $data = $data->with(['user'])->order('is_top', 'desc')->order('sort', 'desc')->order('id', $orderway)->paginate($limit, false, ['page' => $page])->toArray();

        $arList = [];
        foreach ($data['data'] as $v) {
            $commentData = Comment::where('pid', 0)->where('aid', $v['id'])
                ->where('type', 'video')
                ->with(['user', 'children'])
                ->limit(5)->select();

            $commentList = [];
            foreach ($commentData as $ck => $cv) {
                $commentList[$ck] = $cv->toArray();
                $commentList[$ck]['createtime'] = date('Y-m-d H:i:s', $commentList[$ck]['createtime']);
                if (!$cv['user']) {
                    $cv['user'] = User::getSoftDeleteUser($cv['user_id']);
                }
                $commentList[$ck]['user'] = [
                    'id' => $cv['user']['id'],
                    'username' => $cv['user']['nickname'],
                    'avatar' => $cv['user']['avatar'],
                ];
                $commentList[$ck]['is_children'] = $cv['children'] ? 1 : 0;//是否存在子集|0:否,1:是
            }

            $is_mylike = UserLike::where('table_name', 'video')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->find();
            $is_myfavorite = UserFavorite::where('table_name', 'video')
                ->where('user_id', $user['id'])
                ->where('object_id', $v['id'])
                ->find();

            if ($v['image']) {
                if (substr($v['image'], 0, 1) === '/') {
                    $v['image'] = obsurl($v['image']);
                }
            }
            if ($v['video_path']) {
                if (substr($v['video_path'], 0, 1) === '/') {
                    $v['video_path'] = obsurl($v['video_path']);
                }
            }

            if (!$v['user']) {
                $v['user'] = User::getSoftDeleteUser($v['user_id']);
            }

            if ($v['user']['avatar']) {
                $v['user']['avatar'] = obsurl($v['user']['avatar']);
            }

            //内容数组
            $content_arr = [
                'id' => $v['id'],
                'title' => $v['title'],//标题
                'description' => $v['description'],//描述
                'image' => $v['image'],//封面图片
                'video_path' => $v['video_path'],//视频链接
                'is_top' => $v['is_top'],//是否置顶|0:否
                'type' => $v['type'],//类型：1视频2音频
                'post_like' => $v['count_like'] ?? 0,//点赞数量
                'post_hits' => $v['count_view'] ?? 0,//点击量
                'post_favorites' => $v['count_favorite'] ?? 0,//收藏数量
                'comment_count' => $v['count_comment'],//评论数量
                'is_mylike' => $is_mylike ? 1 : 0,//是否喜欢|0:否
                'is_myfavorite' => $is_myfavorite ? 1 : 0,//是否收藏|0:否
                'user_id' => $v['user_id'],//用户id
                'thumbnail' => $v['user']['avatar'],//头像
                'username' => $v['user']['nickname'],//昵称
                'commentList' => $commentList,//顶级评论列表
                'create_time' => $v['create_time'],//创建时间
                'post_forward' => $v['count_forward'] ?? 0,//转发数量
                'membership_price' => $v['membership_price'],//会员价格|0:免费
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
            ];

            //判断内容是否收费
            if (in_array($v['video_category_id'], $free_catedory_id_arr) && (float)$v['membership_price'] >= 0.01) {
                //内容收费
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'articles', 'video', $v['id']);
                //判断开没开内容会员
                if ($isMemberExpire['code'] != 200) {
                    //没开内容会员
                    $content_arr['description'] = '';
                    $content_arr['image'] = '';
                    $content_arr['video_path'] = '';
                    $content_arr['is_member_expire'] = 1;//会员是否到期|0:否,1:是
                }
            }
            $arList[] = $content_arr;
        }

        //获取专辑
        $albumList_model = AlbumModel::where('table_name', 'video_category')->whereIn('category_id', $category_id_arr)->order('sort', 'desc')->select();
        foreach ($albumList_model as $v) {
            //内容数组
            $content_arr = [
                'id' => $v['id'],//专辑id
                'name' => $v['name'],//专辑名称
                'introduction' => $v['introduction'],//介绍
                'cover_image' => obsurl($v['cover_image']),//封面图url
                'table_name' => $v['table_name'],//分类类型|数据源不加前缀的表名|video_category=视频
                'category_id' => $v['category_id'],//所属分类id数据源不加前缀的表中的id|专辑归属的分类id
                'membership_price' => $v['membership_price'],//会员价格(元)|默认0:免费
                'membership_duration' => $v['membership_duration'],//会员天数|默认0天
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
            ];
            //分类免费
            if (in_array($v['category_id'], $free_catedory_id_arr) && (float)$v['membership_price'] >= 0.01) {
                //内容收费
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'album', 'album', $v['id']);
                //判断开没开内容会员
                if ($isMemberExpire['code'] != 200) {
                    //没开内容会员
                    $content_arr['is_member_expire'] = 1;
                }
            }
            $albumList[] = $content_arr;
        }

        $res = [
            'total' => $data['total'],
            'videoList' => $arList,
            'albumList' => $albumList ?? [],//专辑列表
        ];
        return $res;
    }

    //获取直播-首页内容列表
    private function getLiveHome($user, $category_id_arr, $free_catedory_id_arr, $page, $limit, $keyword, $time_arr, $type, $orderway, $membershipExpiryRecord)
    {

        $data = Live::whereIn('live_category_id', $category_id_arr);

        if ($keyword) {
            $data->where('title', 'like', "%{$keyword}%");
        }

        $time = $time_arr['time'];
        if ($time) {
            $data->where('live_time', '<=', strtotime($time));//开播时间
        }

        switch ($type) {
            case 1:
                $end_time = date('Y-m-d 23:59:59');
                break;
            case 2:
                $end_time = date('Y-m-d', strtotime($time . ' + 7 days'));
                break;
            case 3:
                $end_time = date('Y-m-d', strtotime($time . ' + 31 days'));
                break;
            case 4:
                $end_time = date('Y-m-d', strtotime($time . ' + 365 days'));
                break;
            default:
                $end_time = date('Y-m-d H:i:s', time());
        }

        $getStartAndEndTime = $this->getStartAndEndTime($type);//根据类型获取日期时间
        $data->where('live_time', '<=', strtotime($getStartAndEndTime['end_time']));

        $data = $data->order('is_top', 'desc')->order('live_time', $orderway)->paginate($limit, false, ['page' => $page])->toArray();

        $arList = [];
        foreach ($data['data'] as $v) {
            //内容数组
            if ($v['image']) {
                if (substr($v['image'], 0, 1) === '/') {
                    $v['image'] = obsurl($v['image']);
                }
            }
            $content_arr = [
                'id' => $v['id'],
                'title' => $v['title'],//标题
                'description' => $v['description'],//描述
                'image' => $v['image'],//封面图片
                'live_url' => $v['live_url'],//直播地址
                'live_time' => $v['live_time'],//直播时间
                'is_top' => $v['is_top'],//是否置顶|0:否
                'is_start_live' => $v['is_start_live'],//是否开始直播|0:否
                'create_time' => $v['create_time'],//创建时间
                'membership_price' => $v['membership_price'],//会员价格|0:免费
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
            ];

            //判断内容是否收费
            if (in_array($v['live_category_id'], $free_catedory_id_arr) && (float)$v['membership_price'] >= 0.01) {
                //内容收费
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'articles', 'live', $v['id']);
                //判断开没开内容会员
                if ($isMemberExpire['code'] != 200) {
                    //没开内容会员
                    $content_arr['description'] = '';
                    $content_arr['image'] = '';
                    $content_arr['live_url'] = '';
                    $content_arr['is_member_expire'] = 1;//会员是否到期|0:否,1:是
                }
            }
            $arList[] = $content_arr;
        }
        $res = [
            'liveList' => $arList,
            'total' => $data['total']
        ];
        return $res;
    }

    //获取投研日志-首页内容列表
    private function getInvestmentHome($user, $category_id_arr, $free_catedory_id_arr, $page, $limit, $keyword, $time_arr, $type, $orderway, $membershipExpiryRecord)
    {
        $category_ids = InvestmentCategoryModel::column('id');
        $data['data'] = [];
        foreach ($category_ids as $v) {
            $model = Investment::where('investment_category_id', $v);
            if ($keyword) {
                //查询分词索引表
                $cut_word_index = new \app\api\service\cms\CutWordIndex();
                $a_id = $cut_word_index->getCutWordIndex($keyword, 'investment')['a_id'];
                if ($a_id) {
                    $model = $model->whereIn('id', $a_id);
                }

                //$model = $model->where('macro_content', 'like', "%{$keyword}%");//旧业务逻辑
            }
            $start_time = $time_arr['start_time'];
            $end_time = $time_arr['end_time'];
            if ($start_time) {
                $model = $model->where('createtime', '>=', strtotime($start_time));
            }
            if ($end_time) {
                $model = $model->where('createtime', '<=', strtotime($end_time));
            }
            $model = $model->order('id', 'desc')->limit(2)->select();
            $data['data'] = array_merge($data['data'], $model);
            $data['total'] = count($data['data']);
        }
        $arList = [];
        foreach ($data['data'] as $v) {

            //判断是否分类是只显示文件的
            $is_show_file = InvestmentCategoryModel::where('id', $v['investment_category_id'])->value('type');
            $file_list = [];
            if ($is_show_file == 1) {
                $file_list = json_decode($v['file_list'], true) ?? [];
                foreach ($file_list as &$fv) {
                    $fv['url'] = obsurl($fv['url']);
                }
            }

            $file_img = [];
            foreach (explode(',', $v['file_img']) as $path) {
                $file_img[] = obsurl($path);
            }
            $file_video = [];
            foreach (explode(',', $v['file_video']) as $path) {
                $file_video[] = obsurl($path);
            }
            $file_audio = [];
            foreach (explode(',', $v['file_audio']) as $path) {
                $file_audio[] = obsurl($path);
            }
            $file_data = [];
            if ($v['file_data']) {
                $attachment = Attachment::whereIn('url', $v['file_data'])->select();
                foreach ($attachment as $av) {
                    $file_data[] = [
                        'filename' => $av['filename'],
                        'url' => obsurl($av['url'])
                    ];
                }
            }


            //内容数组
            $content_arr = [
                'id' => $v['id'],
                'macro_content' => $v['macro_content'],//宏观面
                'policy_content' => $v['policy_content'],//政策面
                'fund_content' => $v['fund_content'],//资金面及预期
                'emotion_content' => $v['emotion_content'],//情绪面
                'expect_content' => $v['expect_content'],//市场预期/关注
                'news_content' => $v['news_content'],//市场新闻
                'file_img' => array_filter($file_img),//
                'file_img_num' => count(array_filter($file_img)),//图片数组
                'file_video' => array_filter($file_video),//
                'file_audio' => array_filter($file_audio),//
                'file_data' => array_filter($file_data),//
                'content_time' => date('Y-m-d H:i:s', $v['content_time']),//时间
                'create_time' => date('Y-m-d H:i:s', $v['createtime']),//创建时间
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
                'is_show_file' => $is_show_file,//0=不展示文件,1=展示文件
                'file_list' => $file_list,
            ];

            $arList[] = $content_arr;
        }
        $res = [
            'investmentList' => $arList,
            'total' => $data['total']
        ];
        return $res;
    }

    //获取量化信号(图表)-首页内容列表
    private function getChartHome($user, $category_id_arr, $free_catedory_id_arr, $page, $limit, $keyword, $time_arr, $type, $orderway, $membershipExpiryRecord)
    {
        //判断开始和结束日期是否使用默认
        $start_time = $time_arr['time'];
        $end_time = $time_arr['end_time'];
        if (!$start_time || !$end_time) {
            //默认最近一年
            $start_time = date('Y-m-d', strtotime('-12 months'));
            $end_time = date('Y-m-d');
        }
        $data = Diyform::whereIn('parent_id', $category_id_arr);
        //获取每张图表需要的字段
        $data = $data->order('id', 'desc')->paginate($limit, false, ['page' => $page])->toArray();
        //根据这些字段去对应的数据表中获取坐标
        $arList = [];
        foreach ($data['data'] as $v) {
            $fields = [];
            foreach ($v['fields'] as $fsv) {
                $fields = array_merge($fields, json_decode(trim($fsv, '[]'), true));
            }
            $field = [];
            foreach ($fields as $fields_k => $fields_v) {
                $field[] = $fields_k;
            }
            $field = array_merge($field, ['datetime']);
            //获取对用的图表坐标数据
            $chartInfo = Db::table($v['table'])->field($field)->select();

            $res_data = [];
            $datetime = [];
            //循环坐标数据,将相同字段的数据放在一起
            foreach ($chartInfo as $cv) {
                $datetime[] = date('Y-m-d', strtotime($cv['datetime']));
                foreach ($fields as $fk => $fv) {
                    $res_data[$fk][] = $cv[$fk];
                }
            }

            $chartInfo_data = [];
            $y_axial_2_data = [];//双Y轴数据
            //循环相同的坐标数据,将每个坐标数据进行充重组成新的数据给前端
            foreach ($res_data as $rk => $rv) {
                if ($rk == $v['y_axial_2']) {
                    $y_axial_2_data = [
                        'name' => $datetime,
                        'val' => $rv,
                        'line' => $fields[$rk],
                        'min_interval' => (float)$v['min_interval'],
                        'max_interval' => (float)$v['max_interval'],
                    ];
                } else {
                    $y_axial_2_data = [];
                }
                $chartInfo_data[] = [
                    'name' => $datetime,
                    'val' => $rv,
                    'line' => $fields[$rk],
                    'min_interval' => (float)$v['min_interval'],
                    'max_interval' => (float)$v['max_interval'],
                    'y_axial_2_data' => $y_axial_2_data,
                ];
            }

            //内容数组
            $content_arr = [
                'id' => $v['id'],
                'name' => $v['name'],
                'title' => $v['title'],
                'data' => $chartInfo_data,
                'membership_price' => $v['membership_price'],//会员价格|0:免费
                'is_member_expire' => 0,//会员是否到期|0:否,1:是
            ];
            //判断内容是否收费
            if (in_array($v['parent_id'], $free_catedory_id_arr) && (float)$v['membership_price'] >= 0.01) {
                //内容收费
                $isMemberExpire = $membershipExpiryRecord->isMemberExpire($user['id'], 'articles', 'pro_diyform', $v['id']);
                //判断开没开内容会员
                if ($isMemberExpire['code'] != 200) {
                    //没开内容会员
                    $content_arr['data'] = '';
                    $content_arr['is_member_expire'] = 1;//会员是否到期|0:否,1:是
                }
            }
            $arList[] = $content_arr;
        }
        $res = [
            'chartList' => $arList,
            'total' => $data['total']
        ];
        return $res;
    }


    //获取二级分类
    public function getCategoryList2()
    {
        $pid = $this->request->param('channels_id', 0);
        $user = $this->auth->getUser();//用户信息
        $column = Channels::where('status', 'in', [1, 2, 3]);
        if ($pid) {
            $column = $column->where('parent_id', $pid);
        }
        $column = $column->order('list_order', 'desc')
            ->select();
        $data = Channels::buildTree($column, $pid);//获取分类树形结构


        $column_rule_model = new User();
        $column_rule = $column_rule_model->getUserChannels($user);//通过用户模型获取用户的频道
        foreach ($data as $key => &$value) {
            //判断顶级分类id是否在权限组中
            if ($value['status'] == 1 || in_array($value['id'], $column_rule)) {
                $value['authorize'] = true;//是否有权访问|true:是,false:否
            } else {
                if (in_array($value['id'], $column_rule)) {
                    $value['authorize'] = true;
                } else {
                    $value['authorize'] = false;
                }
            }
            //判断2级分类id是否在权限组中
            foreach ($value['children'] as &$vv) {
                //如果顶级分类允许访问,那么2级分类就全部允许访问
                if (in_array($value['id'], $column_rule)) {
                    $vv['authorize'] = true;
                } else {
                    if ($vv['status'] == 1 || in_array($vv['id'], $column_rule)) {
                        $vv['authorize'] = true;
                    } else {
                        $vv['authorize'] = false;
                    }
                }
            }
        }

        $this->success('获取成功', $data);
    }

    //文章转载
    public function reprintPost()
    {
        $request = $this->request->param();//获取提交的参数
        $user = $this->auth->getUser();//用户信息
        $post_id = $request['post_id'];//原文章id
        $category_type = $request['category_type'];//类型|plaza:广场帖子,plate:论坛帖子,news:新闻
        $title = $request['title'] ?? null;//标题

        $file_img = str_replace("\n", '', ($request['fileImgs'] ?? null));//图片,多个文件用英文逗号分割
        $file_video = str_replace("\n", '', ($request['fileVideos'] ?? null));//视频,多个文件用英文逗号分割
        $file_audio = str_replace("\n", '', ($request['fileMp3'] ?? null));//音频,多个文件用英文逗号分割
        $file_data = str_replace("\n", '', ($request['file'] ?? null));//文件

        $content = $request['content'];//文章内容

        $rule = [
            'content|内容' => 'require',
            'post_id|原文章id' => 'require',
        ];
        $msg = [
            'content.require' => '内容不能为空',
            'post_id.require' => '原文章id不能为空',
        ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check($request);
        if (!$result) {
            $this->error($validate->getError());
        }

        //$content = $this->markdownToHtml($content);//Markdown 转 富文本html  @todo 前端富文本组件不用了 2024-02-21 17:13


        $post_keywords = $request['post_keywords'] ?? null;//关键词|category_type=news用
        $post_excerpt = $request['post_excerpt'] ?? null;//post摘要|category_type=news用
        $post_source = $request['post_source'] ?? null;//转载文章的来源|category_type=news用

        switch ($category_type) {
            case 'plaza':
                //获取原文内容
                $old_model = PlazaPostModel::where('id', $post_id)->find();
                $category_id = $old_model['plaza_category_id'];
                $this->addPlaza($user, $category_id, $content, $title, $post_id, $file_img, $file_video, $file_data);
                break;
            case 'plate':
                $old_model = Platepost::where('id', $post_id)->find();
                $category_id = $old_model['plateid'];
                $this->addPlate($user, $category_id, $content, $title, $post_id, $file_img, $file_video, $file_data, $file_audio);
                break;
            case 'news':
                $old_model = News::where('id', $post_id)->value('post_content');
                $category_id = $old_model['parent_id'];
                $this->addNews($user, $category_id, $content, $title, $post_keywords, $post_excerpt, $post_source, $post_id, $file_img, $file_video, $file_data);
                break;
            default:
                $this->error('类型不正确');
        }
    }

    //加密a标签链接
    private function toBase64($id, $arr, $title)
    {
        $str = json_encode($arr);
//        $content = base64_encode(urldecode(preg_replace_callback('/%([0-9A-F]{2})/', function ($matches) {
//            return chr(hexdec($matches[1]));
//        }, $str)));
        $html_a = "<a href='https://fupanai.com/h5/#/home/forumDetails?id={$id}&type={$arr['category_type']}&comment_type={$arr['comment_type']}&status=1' target='_blank'>【转载--】{$title}</a>";
        return $html_a;

    }

    //判断文章内容是否是转载 1:是,0:否
    private function is_reprint($content)
    {
        $prefix = '【转载--';
        $is_reprint = strpos($content, $prefix) === 0;
        return $is_reprint ? 1 : 0;
    }

}
